欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義控件的創(chuàng)建方法

 更新時(shí)間:2017年04月10日 17:10:08   作者:安小松丶  
這篇文章主要為大家詳細(xì)介紹了Android自定義控件的創(chuàng)建方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了Android創(chuàng)建自定義控件的具體代碼,供大家參考,具體內(nèi)容如下

1、仿iPhone 的風(fēng)格,在界面的頂部放置一個(gè)標(biāo)題欄。

<?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="match_parent"
 >

 <LinearLayout
  android:layout_width="match_parent"
  android:layout_height="50dp"
  android:background="#2197db"
  android:orientation="horizontal"
  android:layout_alignParentTop="true"
  android:layout_alignParentLeft="true"
  android:layout_alignParentStart="true">

  <Button
   android:id="@+id/title_back"
   android:layout_width="90dp"
   android:layout_height="40dp"
   android:layout_gravity="center"
   android:layout_margin="5dp"
   android:text="返回"
   />

  <TextView
   android:id="@+id/title_text"
   android:layout_width="0dp"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:layout_weight="1"
   android:gravity="center"
   android:text="標(biāo)題"
   android:textColor="#fff"
   android:textSize="24sp"
   />
  <Button
   android:id="@+id/title_edit"
   android:layout_width="90dp"
   android:layout_height="40dp"
   android:layout_gravity="center"
   android:layout_margin="5dp"
   android:text="確定"
   />

 </LinearLayout>
</RelativeLayout>

標(biāo)題欄布局已經(jīng)編寫完成,剩下的就是如何在程序中使用這個(gè)標(biāo)題欄。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/title" />
</LinearLayout>
//我們只需要通過一行 include語句將標(biāo)題欄布局引入進(jìn)來就可以了。

然后在 MainActivity 中將系統(tǒng)自帶的標(biāo)題欄隱藏掉

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
}

我們還是需要在每個(gè)活動(dòng)中為這些控件單獨(dú)編寫一次事件注冊的代碼。比如說標(biāo)題欄中的返回按鈕,其實(shí)不管是在哪一個(gè)活動(dòng)中,這個(gè)按鈕的功能都是相同的,即銷毀掉當(dāng)前活動(dòng),這種情況最好是使用自定義控件的方式來解決。

新建自定義的標(biāo)題欄控件:

public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.title, this);
}
}


我們重寫了 LinearLayout 中的帶有兩個(gè)參數(shù)的構(gòu)造函數(shù),在布局中引入 TitleLayout控件就會(huì)調(diào)用這個(gè)構(gòu)造函數(shù)。然后在構(gòu)造函數(shù)中需要對標(biāo)題欄布局進(jìn)行動(dòng)態(tài)加載,這就要借助 LayoutInflater 來實(shí)現(xiàn)了。通過 LayoutInflater 的 from()方法可以構(gòu)建出一個(gè) LayoutInflater對象,然后調(diào)用 inflate()方法就可以動(dòng)態(tài)加載一個(gè)布局文件,inflate()方法接收兩個(gè)參數(shù),第一個(gè)參數(shù)是要加載的布局文件的 id,這里我們傳入 R.layout.title,第二個(gè)參數(shù)是給加載好的布局再添加一個(gè)父布局,這里我們想要指定為 TitleLayout,于是直接傳入this

在布局文件中添加這個(gè)自定義控件 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.xxxxxx.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
></com.example.xxxxxx.TitleLayout>
</LinearLayout>

我們來嘗試為標(biāo)題欄中的按鈕注冊點(diǎn)擊事件,修改 TitleLayout中的代碼

public class TitleLayout extends LinearLayout {
 public TitleLayout(Context context, AttributeSet attrs) {
  super(context, attrs);
  LayoutInflater.from(context).inflate(R.layout.title, this);
  Button titleBack = (Button) findViewById(R.id.title_back);
  Button titleEdit = (Button) findViewById(R.id.title_edit);

  titleBack.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    ((Activity) getContext()).finish();
   }
  });

  titleEdit.setOnClickListener(new OnClickListener() {
   public static final String TAG = "";

   @Override
   public void onClick(View v) {
    Toast.makeText(getContext(), "重新運(yùn)行程序", Toast.LENGTH_SHORT).show();
    Log.i(TAG, "111 ");
   }
  });
 }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論