初学_Android4高级编程-1 Application单例,及优化布局

※Application
每次应用程序运行时,Application类保持实例化状态,Application实现为一个单态(单例),扩展如:
import *;
public class MyApplication extends Application{
private static MyApplication singleton;

public static MyApplication getIntance(){
return singleton;
}

//创建应用程序是调用,实例化应用程序单态,创建和实例化状态变量或共享资源
@Override
public final void onCreate(){
super.onCreate();
singleton = this;
}
//当系统处于资源匮乏的状态时,清空缓存和不必要资源
@Override
public final void onLowMemory(){
super.onLowMemory();
}
//当系统决定减少内存开销时调用,包含一个level参数,永于提供请求上下文
@Override
public final void onTrimMemoy(int level){
super.onTrimMemoy(level);
}
@Override
//当应用程序使用的值依赖于特定的配置,重写来重新加载这些值。
public final void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
}

※优化布局
1.当包含有merge标签的布局被添加到另一个布局是,该布局的merge会被删除。
使用include标签把包含于merge标签的布局添加到另一个布局中,能够创建灵活的,可复用的布局定义。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include android:id="@+id/my_image_text_layout"
layout="@layout/image_text_layout"
</LinearLayout>

image_text_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:gravity="center_horizontal"
android:layout_gravity="bottom"
/>
</merge>

2.避免使用多个View,布局包含的View个数不用改超过80。想要在复杂的布局内填充View
的数量较少,可以使用ViewStub。相当于一个延迟的include标签,只有在显示的调用inflate()方法或被置为可见时才会被填充。
View stub = findViewById(R.id.id_stub);
stub.setVisibility(View.VISIBLE);
View inStub = findViewById(R.id.id_in_stub)

ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);
stub.inflate();

//layout属性为需要填充的xml文件。
<ViewStub
android:id="@+id/id_stub"
android:inflatedId="@+id/id_in_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/viewstub_layout"/>

时间: 2024-08-08 03:28:30

初学_Android4高级编程-1 Application单例,及优化布局的相关文章

初学_Android4高级编程-2 Fragment

Fragment允许将activity拆分成多个完全独立封装的可重用控件.每个组件有它自己的生命周期和UI布局.(类似于一个嵌入在activity中的activity)通过继承Fragment来创建一个新的Fragment,大多数情况下,需要为Fragment分配一个UI(xml布局文件),也可以创建一个没有任何UI的后台行为的Fragment. public class MyFragment extends Fragment{ //调用该方法连接它的父Activity @Override pu

初学_Android4高级编程-3 自定义View

※创建自定义控件1.修改现有视图,继承android自带控件,扩展自带控件来定制其外观和行为.2.直接继承View,SurfaceView类,在一个空画布上创建新的控件 由于效率问题,在自定义的构造函数中完成画刷的创建.public MyTextVIew(Context context) { super(context); Resources resources = getResources(); marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG); m

初学_Android4高级编程-4 简单的Adapter

※Adapter ·ArrayAdapter 使用泛型把Adapter视图绑定到一个指定类的对象的数组.使用一个对象数组的每个元素的toString值来填充指定布局中的TextView.ArrayList<String> item = new ArrayList<>();ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this,R.layout.item_list,item); ·SimpleAd

【Java编程】 设计模式 -- 单例设计模式

思想: 为了避免其他程序过多建立该类对象,先禁止其他程序建立该类对象 为了让其他程序可以访问到该类对象,只好在本类中自定义一个对象 为了方便其他程序对自定义对象的访问,可以对外提供一些访问方式 代码体现: 将构造函数私有化 在类中创建一个本类对象 提供一个方法可以获取到该对象 代码一 先初始化对象 称为:饿汉式 Single类一进内存,就已经创建好了对象. public class SingleModeDemo { public static void main(String[] args){

MVC5高级编程_表单和html辅助方法

使用HTML辅助方法关键在于 确保HTML页面链接中的URL指向正确的位置.表单元素拥有适用于模型绑定的合适名称和值,以及当模型 绑定失败时其他元素能够显示相应的错误提示消息. 1.表单的使用     @using(Html.BeginForm("Searhch", "Home", FormMethod.Get,new { target = "_blank", @class="editForm", data-validatab

ASP.NET MVC5高级编程 之 表单

1.1 action和method特性 表单是包含输入元素的容器,其中包含按钮.复选框.文本框等元素.表单中的这些输入元素使得用户能够向页面中输入信息,并把输入的信息提交给服务器.数据的提交依赖于action和method. action特性用以告知Web浏览器信息发送到哪里,所以action包含一个URL.这里的URL可以是相对的,也可以使绝对的. 1 <form action="http://www.bing.com/search"> 2 <input name=

java-并发编程-多线程设计模式-单例设计模式的七种写法

1.饿汉式 public class SingleInstance{ private static final SingleInstance instance = new SingleInstance(); private SingleInstance(){}; public static SingleInstance getInstance(){ return instance; }; } 这种写法的单例模式是最简单的设计模式,基本上大部分开发者都会写. 2.懒汉式 3.懒汉式+同步 4.Du

python高级编程(第12章:优化学习)1

# -*- coding: utf-8 -*-# python:2.x__author__ = 'Administrator'#由于5,6,7,8,9,10,11主要是在包,测试之类的学习所以这边就不学习 #优化:通用原则和剖析技术print(u'过早进行优化是编程中万恶之源-------Donald Knuth') #1:优先三原则"""无论结果如何,优化是需要代价的,当代码能够正常工作时,不用会理会它(有时)可能比不惜一切代价尝试让它运行得更快要好一些当优化代码,需要3条

python高级编程(第12章:优化学习)3

#微观剖析 ''' 当找到速度很慢函数时,有时还需要做到测试某个部分函数剖析工作,这需要通过手动对一部分代码速度测试完成 ''' """ import tempfile,os,pstats,cProfile def p1(column='time',list1=5): def _p1(function): def __p1(*a,**k): s=tempfile.mktemp() p2=cProfile.Profile() p2.runcall(function,*a,**k