自定义标题栏格式

android 自定义标题栏

 

  • 修改标题栏的宽度和背景

在strings.xml中添加:
[html] view plaincopyprint? <item name=”android:background”>@drawable/title_bg</item>
</style>

<style name=”test” parent=”android:Theme”>
<item name=”android:windowTitleSize”>40dp</item>
<item name=”android:windowTitleBackgroundStyle”>@style/CustomWindowTitleBackground</item>
</style>
<item name=”android:background”>@drawable/title_bg</item>
</style>

<style name=”test” parent=”android:Theme”>
<item name=”android:windowTitleSize”>40dp</item>
<item name=”android:windowTitleBackgroundStyle”>@style/CustomWindowTitleBackground</item>
</style>

  • 修改AndroidMainfest.xml文件:

[html] view plaincopyprint?<activity android:name=”.MainActivity”
android:theme=”@style/test”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.MainActivity”
android:theme=”@style/test”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>

 

  •  MainActivity:

[html] view plaincopyprint?public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); //声明使用自定义标题
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);//自定义布局赋值
}

 

  •  layout文件夹中添加title.xml文件,内容如下:

[html] view plaincopyprint?<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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_centerVertical=’true’
android:layout_height=”wrap_content”
android:src=”@drawable/ic_launcher_email” />
<TextView android:layout_width=”wrap_content”
android:layout_centerInParent=”true”
android:layout_height=”wrap_content”
android:text=”自定义标题栏” />

</RelativeLayout>
<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout 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_centerVertical=’true’
android:layout_height=”wrap_content”
android:src=”@drawable/ic_launcher_email” />
<TextView android:layout_width=”wrap_content”
android:layout_centerInParent=”true”
android:layout_height=”wrap_content”
android:text=”自定义标题栏” />

</RelativeLayout>

    原文作者:lisalty
    原文地址: https://blog.csdn.net/lisalty/article/details/8762763
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞