跑馬燈 自定義XML
跑馬燈
布局
- 一個繼承于Activity的類,的xml文件,創(chuàng)建一個
<GridView
android:id "@+id/gv"
android:layout_width "match_parent"
android:layout_height "0dp"
android:layout_weight "1"
android:verticalSpacing "20dp"
android:numColumns "3"
></GridView>
- GridView需要一個Adapter,創(chuàng)建一個繼承于BaseAdapter的類
class HomeAdapter extends BaseAdapter{
private String[] mItems =new String[]{
"手機防盜",
"通訊衛(wèi)士",
"軟件管理",
"進程管理",
"流量統(tǒng)計",
"手機殺毒",
"緩存清理",
"高級工具",
"設置中心"
};
private int[] mImages = new int[]{
R.drawable.home_safe,
R.drawable.home_callmsgsafe,
R.drawable.home_apps,
R.drawable.home_taskmanager,
R.drawable.home_netmanager,
R.drawable.home_trojan,
R.drawable.home_sysoptimize,
R.drawable.home_tools,
R.drawable.home_settings
};
@Override
public int getCount() {
return mItems.length;
}
@Override
public Object getItem(int arg0) {
return mItems[arg0];
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
View view = View.inflate(ShouYeActivity.this, R.layout.home_list_item, null);
ImageView ivItem = (ImageView) view.findViewById(R.id.imageView1);
TextView textView = (TextView) view.findViewById(R.id.textView1);
textView.setText(mItems[arg0]);
ivItem.setImageResource(mImages[arg0]);
return view;
}
}
- Adapter中的布局文件XML--home_list_item,這個XML文件定義的是GridView中一個item的樣式。
<LinearLayout xmlns:android "http://schemas.android.com/apk/res/android"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:gravity "center"
android:orientation "vertical" >
<ImageView
android:id "@+id/imageView1"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:src "@drawable/home_apps" />
<TextView
android:id "@+id/textView1"
android:layout_width "wrap_content"
android:layout_marginTop "5dp"
android:textColor "#000"
android:textSize "16sp"
android:layout_height "wrap_content"
android:text "手機防盜" />
</LinearLayout>
示例
示例圖片1
示例圖片2
- 自定義XML文件 view_setting_item.xml
<TextView
android:id "@+id/tv_title"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:textColor "#000"
android:textSize "22sp" />
<TextView
android:id "@+id/tv_desc"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:layout_below "@id/tv_title"
android:layout_marginTop "3dp"
android:textColor "#a000"
android:textSize "18sp" />
<CheckBox
android:id "@+id/cb"
android:clickable "false"
android:focusable "false"
android:layout_width "wrap_content"
android:layout_height "wrap_content"
android:layout_alignParentRight "true"
android:layout_centerVertical "true" />
<View
android:layout_width "match_parent"
android:layout_height "0.2dp"
android:layout_alignParentBottom "true"
android:background "#a000" />
</RelativeLayout>
- 自定義一個RelativeLayout的類SettingItemView,并且實現(xiàn)構造方法
public class SettingItemView extends RelativeLayout{
private TextView de;
private CheckBox cb;
private TextView textView;
public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public SettingItemView(Context context) {
super(context);
initView();
}
//初始化布局
//獲得上下文對象getContext()
private void initView(){
//將自定義好的布局文件設置給當前的settingItemView
View view = View.inflate(getContext(), R.layout.view_setting_item, this);
textView = (TextView) findViewById(R.id.tv_title);
de = (TextView) findViewById(R.id.tv_desc);
cb = (CheckBox) findViewById(R.id.cb);
}
//構造一個設置view上的textView文字的方法
public void setTitle(String title) {
textView.setText(title);
}
//構造一個設置view上的textView文字的方法
public void setDe(String desc) {
de.setText(desc);
}
//構造一個查看chcekBox是否是選擇的狀態(tài)。
public boolean isChecked() {
return cb.isChecked();
}
//構造一個設置chcekBox選擇狀態(tài)的方法
public void setChecked(boolean check) {
cb.setChecked(check);
}
}
- 在Activity的XML文件中(setting),用上面的SettingItemView創(chuàng)建
<?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 "match_parent"
android:orientation "vertical" >
<TextView
style "@style/TitleStyle"
android:text "設置中心" />
<com.example.kongjian.SettingItemView
android:id "@+id/siv_update"
android:layout_width "match_parent"
android:layout_height "wrap_content" />
</LinearLayout>
public class SettingActivity extends Activity{
private SettingItemView sivUpdate;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
sivUpdate = (SettingItemView) findViewById(R.id.siv_update);
sivUpdate.setTitle("自動更新設置");
sivUpdate.setDe("自動更新已開啟");
sivUpdate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//判斷當前的勾選狀態(tài)
if (sivUpdate.isChecked()) {
//設置不勾選
sivUpdate.setChecked(false);
sivUpdate.setDe("自動更新已關閉");
}else {
sivUpdate.setChecked(true);
sivUpdate.setDe("自動更新開啟");
}
}
});
}
}