国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android自定義控件的創建方法

2019-12-12 03:08:27
字體:
來源:轉載
供稿:網友

本文為大家分享了Android創建自定義控件的具體代碼,供大家參考,具體內容如下

1、仿iPhone 的風格,在界面的頂部放置一個標題欄。

<?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="標題"   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>

標題欄布局已經編寫完成,剩下的就是如何在程序中使用這個標題欄。

<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語句將標題欄布局引入進來就可以了。

然后在 MainActivity 中將系統自帶的標題欄隱藏掉

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

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

新建自定義的標題欄控件:

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

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

在布局文件中添加這個自定義控件 

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

我們來嘗試為標題欄中的按鈕注冊點擊事件,修改 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(), "重新運行程序", Toast.LENGTH_SHORT).show();    Log.i(TAG, "111 ");   }  }); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 塔河县| 怀安县| 永新县| 桓仁| 曲周县| 开原市| 名山县| 闵行区| 精河县| 木兰县| 潮州市| 延安市| 百色市| 屏南县| 确山县| 虎林市| 库尔勒市| 稻城县| 威远县| 鹰潭市| 金昌市| 通化县| 宁武县| 旺苍县| 贡觉县| 河间市| 观塘区| 农安县| 北京市| 班戈县| 阿拉善左旗| 义乌市| 阜新| 兴隆县| 开江县| 钦州市| 新宁县| 凭祥市| 嘉义县| 海盐县| 望江县|