android 中的组合控件的设计
生活随笔
收集整理的這篇文章主要介紹了
android 中的组合控件的设计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在開發應用程序的時候,很多時候會使用到幾個重復的控件,例如Android手機的設置界面里面的位置服務里面的每一欄都是組合控件,也就是說多個控件組成一個整體,如下圖所示:
紅色方框里面的是由兩個TextView和一個CheckBox組合而成的一個組合控件,要是能把這兩個控件組合成一個控件在開發過程中就有有很大的方便,
在主布局文件中activity_mian:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><RelativeLayout android:layout_width="match_parent"android:layout_height="80dp" ><TextView android:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:layout_marginLeft="10dp"android:textColor="#000000"android:textSize="20sp"android:text="GOOGLE的位置服務"/><TextView android:id="@+id/tv_desc"android:layout_below="@id/tv_title"android:layout_width="280dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:textColor="#88000000"android:textSize="18sp"android:text="允許應用程序使用來自WALN或移動網絡的數據確定您的大致位置"/><CheckBox android:id="@+id/cb_status"android:layout_width="wrap_content"android:layout_height="wrap_content"android:clickable="false"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="10dp"/><View android:layout_width="match_parent"android:layout_height="0.2dp"android:layout_below="@id/tv_desc"android:background="#000000"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"/></RelativeLayout></RelativeLayout> 在這里實現這一個單元的控件,但是還有兩個單元的控件布局類似,如果還是按照上面的代碼往下寫,就會造成代碼的重復,而且不好看,所以這里就會使用到一個組合控件,很方面的解決這個問題,首先把重復的代碼抽取出來
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="80dp" ><TextView android:id="@+id/tv_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="8dp"android:layout_marginLeft="10dp"android:textColor="#000000"android:textSize="20sp"android:text="GOOGLE的位置服務"/><TextView android:id="@+id/tv_desc"android:layout_below="@id/tv_title"android:layout_width="280dp"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="10dp"android:textColor="#88000000"android:textSize="18sp"android:text="允許應用程序使用來自WALN或移動網絡的數據確定您的大致位置"/><CheckBox android:id="@+id/cb_status"android:layout_width="wrap_content"android:layout_height="wrap_content"android:clickable="false"android:layout_alignParentRight="true"android:layout_centerVertical="true"android:layout_marginRight="10dp"/><View android:layout_width="match_parent"android:layout_height="0.2dp"android:layout_below="@id/tv_desc"android:background="#000000"android:layout_marginLeft="5dp"android:layout_marginRight="5dp"/></RelativeLayout> 創建一個自定義的類ItemView.java,該方法繼承了RelativeLayout,實現了一下三個方法
public ItemView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);initView(context);}public ItemView(Context context, AttributeSet attrs) {super(context, attrs);initView(context);}public ItemView(Context context) {super(context);initView(context);} 在調用這三個方法時都會對調用initView(context)方法對item_view.xml中的三個控件進行初始化。 <span style="white-space:pre"> </span>// 把一個布局文件----》一個View,并且加載在SettingItemView中private void initView(Context context) {View view = View.inflate(getContext(), R.layout.item_view, ItemView.this);cb_status = (CheckBox) view.findViewById(R.id.cb_status);tv_desc = (TextView) view.findViewById(R.id.tv_desc);tv_title = (TextView) view.findViewById(R.id.tv_title);} 要重復利用組合控件里面的三個子控件,就還要實現一下幾個方法,分別是isChecked()判斷CheckBox是否選中,setChecked(boolean checked)設置 CheckBox的狀態,setDesc(String text)修改描述信息,setTitle(String text)修改標題等。 /** 組合控件是否有焦點* */public boolean isChecked(){return cb_status.isChecked();}/** 設置組合控件的狀態* */public void setChecked(boolean checked){cb_status.setChecked(checked);}/** 組合控件更改文字* */public void setDesc(String text){tv_desc.setText(text);}/** 組合控件更改標題* */public void setTitle(String text){tv_title.setText(text);}
然后要使用這個組合控件的話就可以在activity_main.xml布局中直接調用自定義控件了。
package cn.edu.cqu.zhkj;import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;public class MainActivity extends Activity{private ItemView iv_google;private ItemView iv_gps;private ItemView iv_es_gps;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);iv_google = (ItemView) findViewById(R.id.iv_google);iv_gps = (ItemView) findViewById(R.id.iv_gps);iv_es_gps = (ItemView) findViewById(R.id.iv_es_gps);iv_gps.setTitle("GPS衛星");iv_gps.setDesc("允許應用程序使用GPS對您進行定位");iv_es_gps.setTitle("使用增強型GPS");iv_es_gps.setDesc("可以使用服務器輔助GPS(取消選中可以降低網絡使用率)");iv_google.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 判斷是否選中if (iv_google.isChecked()) {iv_google.setChecked(false);}else {iv_google.setChecked(true);}}});iv_gps.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 判斷是否選中if (iv_gps.isChecked()) {iv_gps.setChecked(false);}else {iv_gps.setChecked(true);}}});iv_es_gps.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// 判斷是否選中if (iv_es_gps.isChecked()) {iv_es_gps.setChecked(false);}else {iv_es_gps.setChecked(true);}} });}} 這樣就實現了組合控件的重復使用。
總結
以上是生活随笔為你收集整理的android 中的组合控件的设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奥海科技和华为的关系 奥海拥有华为等大牌
- 下一篇: 横店东磁是做什么的