自定义Activity的标题
Android Activity标题,默认情况下是个TextView,显示应用名称的文字。
如果想把标题定义为带图的或者比较复杂的界面效果,就要用到自定义Activity标题。
示例效果:
需要做的是,首先为标题定义一个布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/tv_home_icon" />
<View android:layout_width="3px" android:layout_height="fill_parent" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="看视频" />
</LinearLayout>
这里面,在图和文字之间加了个占位的View元素,否则文字和图片贴的很近。见代码部分:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值
这里要注意次序,否则可能会引起错误。
完整源代码:
这篇文章上的评论的 RSS feed TrackBack URI