觉得Android自动生成的标题栏太丑,不够大? 想要做和Android流行app那样的好看的还带按键的标题栏?
自定义标题栏其实很简单。 首先创建一个XML文件, 取一个合适的名字(此处为title_main)。然后, 将自己想要的标题栏布局写上去, 例如
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title_layout_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:weightSum="10">
<EditText
android:id="@+id/search_box"
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="match_parent"
android:drawableLeft="@drawable/ic_action_search"
android:textSize="25sp"
android:hint="search"/>"
<Button
android:id="@+id/go"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_height="match_parent"
android:text = "GO"/>
</LinearLayout>
接着在activity的onCreate()方法中加入以下代码
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_main);
提示:这三行代码需要按顺序写
接下来,有的人可能会遇到问题,如何设置标题栏的高度, 使得它能够很好的呈现里面的内容
首先要在res\values\的style.xml文件中写入如下代码,新建一种style
<?xmlversion="1.0" encoding="utf-8"?>
<resources>
<style name="titlestyle"parent="android:Theme.Light">
<item name="Android:windowTitleSize">38dip</item> //标题栏的高度
</style>
</resources>
这样,就基本能够实现标题栏的自定义了
其实还有一种方法就是在activity的onCreate方法中调用
requestWindowFeature(Window.FEATURE_NO_TITLE);
隐藏标题栏
然后,在布局XML文件中加入一个置顶的自定义Layout ;)