天天看點

Android Api Demos登頂之路(四)

今天研究的是自定義标題欄,這個内容比較簡單,核心的代碼就兩句

發出requestWindowFeature的請求

設定自定義的标題欄布局

但必須要注意的是requestWindowFeature一定要放在requestWindowFeature

之前

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_custom_title);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);
           

建立标題欄布局檔案custom_title_1.xml

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

    <TextView
        android:id="@+id/tv_left_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/tv_right_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:paddingRight="5dp"
        android:text="TextView" />

</RelativeLayout>
           

建立CustomTitleActivity類的布局檔案activity_custom_title.xml

<?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="match_parent"
    android:orientation="vertical"
     >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical" >

        <EditText
            android:id="@+id/et_left_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxEms="9"
            android:minEms="9"
            android:text="left" />

        <Button
            android:id="@+id/bt_left_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Change left" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_vertical" >

        <EditText
            android:id="@+id/et_right_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxEms="9"
            android:minEms="9"
            android:text="on the right" />

        <Button
            android:id="@+id/bt_right_title"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="Change right" />
    </LinearLayout>

</LinearLayout>
           

建立CustomTitleActivity類并繼承activity

public class CustomTitleActivity extends Activity {
    private TextView tv_left,tv_right;
    private EditText et_left,et_right;
    private Button bt_left,bt_right;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_custom_title);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);

        tv_left=(TextView) findViewById(R.id.tv_left_title);
        tv_right=(TextView) findViewById(R.id.tv_right_title);
        et_left=(EditText) findViewById(R.id.et_left_title);
        et_right=(EditText) findViewById(R.id.et_right_title);
        bt_left=(Button) findViewById(R.id.bt_left_title);
        bt_right=(Button) findViewById(R.id.bt_right_title);

        bt_left.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_left.setText(et_left.getText().toString());
            }
        });

        bt_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_right.setText(et_right.getText().toString());
            }
        });
    }

}
           

在配置檔案中添加activity與前面的demo類似,不再贅述。

繼續閱讀