Android自定义带按钮标题栏

1.修改res/values中的styles.xml,这是为了自定义app的主题样式,在这里可以设置标题栏的宽度、背景颜色

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="windowTitleBackground">
        <item name="android:background">#337ab7</item>
    </style>
    
    <style name="myTheme" parent="android:Theme.Light">
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/windowTitleBackground</item>
    </style>
    
</resources>


2.修改AndroidMainfest.xml文件中的主题设置项 android:theme 的值为自定义主题myTheme


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lab2016_2_4"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/myTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



3.在Layout 文件下创建标题栏的布局文件,这一文件是为了确定自定义标题栏的大致布局,我把它命名为 mytitle.xml


  <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal"
    >
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="left"
        android:id="@+id/left_button"
        android:layout_centerVertical="true"
        />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="right"
        android:id="@+id/right_button"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        />

</RelativeLayout>



4.修改MainActivity.class中的代码,调用mytitle.xml为标题栏的布局

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.activity_main);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.mytitle);
	}

5.真机运行




注意:

如果报出 -you cannot combine custom title with other feature titles 的错误,建议把styles.xml 中 myTheme的 parent 值改为 android:Theme,具体如下

<style name="myTheme" parent="android:Theme">
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/windowTitleBackground</item>
    </style>

 这时,自定义主题myTheme会继承android:Theme 而不再是android:Theme.Light 。 继承 android:Theme.Light ,主题背景默认为白色,而android:Theme 则为黑色


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值