Android第五天-->建立自訂控制項,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 "); } }); }}