Android Api Demos登顶之路(四)

来源:互联网 发布:中国银行淘宝信用卡 编辑:程序博客网 时间:2024/04/30 16:01

今天研究的是自定义标题栏,这个内容比较简单,核心的代码就两句
发出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类似,不再赘述。

0 0