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
- Android Api Demos登顶之路(四)
- Android Api Demos登顶之路(二)
- Android Api Demos登顶之路(三)
- Android Api Demos登顶之路(五)
- Android Api Demos登顶之路(六)
- Android Api Demos登顶之路(七)
- Android Api Demos登顶之路(八)
- Android Api Demos登顶之路(九)
- Android Api Demos登顶之路(十)Overscan
- Android Api Demos登顶之路(十一)Persistent state
- Android Api Demos登顶之路(十二)Presentation
- Android Api Demos登顶之路(十四)Quick Contacts
- Android Api Demos登顶之路(十五)Receive Result
- Android Api Demos登顶之路(十六)Recreate
- Android Api Demos登顶之路(十七)Redirection
- Android Api Demos登顶之路(十八)Recorder Activities
- Android Api Demos登顶之路(十九)Rotation Animation
- Android Api Demos登顶之路(二十二)SetWallpaper
- Java动态代理一——动态类Proxy的使用
- JavaWeb-过滤器与拦截器的区别
- Java中的queue和deque
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(二)、BitmapDrawable和Bitmap
- Android Api Demos登顶之路(四)
- Android Drawable Resource学习(三)、NinePatchDrawable和NinePatch图片的制作
- Android Drawable Resource学习(四)、LayerDrawable
- 天声人語 20150725
- 日经春秋 20150725
- java正则表达式之课堂内容
- 日经社説 20150725 1票の格差を巡る「嫌な感じ」
- Android Drawable Resource学习(五)、StateListDrawable
- 日经社説 20150725 グローバルな再編時代を迎えたメディア