2015年3月11日 星期三

共用Layout

相同畫面例如天/地 (like UserControl)












要共用的Layout 獨自存成xml檔案
需要用的Layout 再<include>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="vertical">
    .
    .
    .
    <include
        android:id="@+id/include_top"
        layout="@layout/include_top" />
    .
    .
    .
    .
</LinearLayout>

使用 incude裡的控件
        
        //找到被include的layout
        View top = findViewById(R.id.include_top);
        //include layout上的控件
        top.findViewById(R.id.btn_XXX).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
              // do something
            }
        });

沒有留言:

張貼留言