1.获取应用版本信息
try {
//获取包管理器
PackageManager pm = getPackageManager();
//获取包信息(AndroidManifest.xml文件里的所有信息都封装在PackageInfo类里) 参数一:清单文件里的包名 参数二:写0即可
PackageInfo info = pm.getPackageInfo(getPackageName(), 0);
return info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
return "";
}
2.handler.postDelay()执行延时动作 下面这个例子就是延时5秒再弹出吐司
/*
* handler.postDelayed 这个方法的作用是在新线程中延迟指定时间去做某个操作
* 第一个参数:需要延迟做的操作
* 第二个参数:延迟的时间
*/
handler.postDelayed(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "哈哈哈",Toast.LENGTH_SHORT).show();
}
}, 5000);
3.如果希以下两个属性作用于TextView上并且生效,那么就需要自定义一个TextView,继承TextView类,覆写父类的构造方法,最后再覆写isFocused方法,永远返回true即可。
android:ellipsize="marquee"
android:focusableInTouchMode="true"
4.状态选择器 手指按下 离开的时候显示不同颜色或者图片
在res目录下创建drawable文件夹,创建颜色选择器xml文件(home_item_selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/gray" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@color/gray" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="@android:color/transparent"/>
<!-- default -->
</selector>
home_item_selector.xml这个文件里引用的颜色是values文件夹下的colors.xml文件里定义的颜色
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="gray">#33000000</color>
</resources>
最后将home_item_selector.xml文件引入到某个布局文件,例如这个项目中就是引入到九宫格的grid_home_item.xml文件中
android:background="@drawable/home_item_selector"
5.自定义的组合控件:
(1).创建一个自定义的组合控件,继承一个布局RelativeLayout或者LinearLayout等
(2).实现父类的构造函数,创建一个initView方法(用于来显示自定义的控件布局) 在每个构造函数里都要使用下。
View view = View.inflate(getContext(), R.layout.ui_setting_view,this);//this表示挂载到当前自定义组合控件上
(3).声明命名空间 res/应用程序里清单文件的包名 xmlns:自定义名称
xmlns:xxc="http://schemas.android.com/apk/res/com.example.mobilesaf"
(4).在自定义组件里添加自定义属性: 例如 xxc:title="我是标题"
(5).在valuse文件夹下创建attrs.xml 在里面声明自定义的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingView"><!-- 这个name值在引用的时候加在attr的name前边 例如SettingView_title -->
<attr name="title" format="string" />
<attr name="desc_on" format="string" />
<attr name="desc_off" format="string" />
</declare-styleable>
</resources>
(6).在自定义组合控件的java代码里,在两个参数的构造方法里添加如下代码
将自定义的属性 xxc:title之类的 和attrs集合建立对应关系 第二个参数是在R文件里自动生成的,是一个数组,只封装了自定义的属性,名字和attrs.xml中declare-styleab标签中的name属性一致
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingView);
获取自定义属性,设置到原始组件上 参数是attrs.xml中declare-styleab标签中的name属性 + _ attr标签中name属性
String title = a.getString(R.styleable.SettingView_title);
tv_setting_title.setText(title);
项目中SettingView.java 自定义组合控件
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.mobilesaf.R;
/**
* 设置界面里的自定义组件
* 里面有个Title,Content,CheckBox
*/
public class SettingView extends RelativeLayout {
private TextView tv_setting_title;
private TextView tv_setting_desc;
private CheckBox cb_setting_state;
private String desc_on;
private String desc_off;
public SettingView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
public SettingView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SettingView);
String title = a.getString(R.styleable.SettingView_title);
tv_setting_title.setText(title);
desc_on = a.getString(R.styleable.SettingView_desc_on);
desc_off = a.getString(R.styleable.SettingView_desc_off);
}
public SettingView(Context context) {
super(context);
initView();
}
public void initView(){
View view = View.inflate(getContext(), R.layout.ui_setting_view, this);//this表示挂载到当前自定义组合控件上
tv_setting_title = (TextView) view.findViewById(R.id.tv_setting_title);
tv_setting_desc = (TextView) view.findViewById(R.id.tv_setting_desc);
cb_setting_state = (CheckBox) view.findViewById(R.id.cb_setting_state);
}
/**
* 设置标题内容
* @param text
*/
public void setTitle(String text){
tv_setting_title.setText(text);
}
/**
* 设置描述内容
* @param text
*/
public void setDesc(String text){
tv_setting_desc.setText(text);
}
/**
* 获取多选框选中状态
* @return
*/
public boolean isChecked(){
return cb_setting_state.isChecked();
}
/**
* 设置复选框状态,并设置相应的描述信息
* @param checked
*/
public void setCheck(boolean checked){
cb_setting_state.setChecked(checked);
if(checked){
setDesc(desc_on);
}else{
setDesc(desc_off);
}
}
}