Android第五天-->建立自訂控制項,android第五天

來源:互聯網
上載者:User

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 ");            }        });    }}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.