setFeatureInt、android 自定义标题栏

setFeatureInt、android 自定义标题栏

Android 自带的toolbar 往往不能很好的的满足我们的个性化要求。因此我们经常使用自定的的标题栏。而Android系统本身也允许我们自定以标题栏。

记录一下,自定义标题栏常遇到的问题。先上效果图:

 

实现起来也很简单。在Activity 的 setContentView方法前添加  

   requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

其含义是允许用户自定义标题栏。

然后再在setContentView后添加

getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_navigation_bar);
其中 R.layout.custom_navigation_bar 为我们自定义的标题栏样式,

 View Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:orientation="vertical"
    tools:context=".BaseActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:padding="10dp">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="left|center_vertical"
            android:paddingBottom="2dp"
            android:paddingTop="2dp">

            <ImageView
                android:id="@+id/navigation_left"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@mipmap/rt_tianjiaguanzhu" />

            <TextView
                android:id="@+id/navigation_left_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="散文"
                android:textSize="16sp" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:paddingBottom="2dp"
            android:paddingTop="2dp">

            <TextView
                android:id="@+id/navigation_center"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginRight="5dp"
                android:text="全部关注"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="16sp" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/icon_down" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="right|center_vertical"
            android:paddingBottom="2dp"
            android:paddingTop="2dp">

            <ImageView
                android:id="@+id/navigation_right"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:src="@mipmap/settings" />
        </LinearLayout>

    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/lightgray" />

</LinearLayout>

最后设置主题,也只最容易出错的地方。对使用该方式添加标题栏的Activity 添加主题样式。

这是我的主题样式 应继承  android:Theme.Light 主题否则会报错。

 View Code

<!-- 标题栏样式 -->
    <style name="WindowTitleBackground" >
        <item name="android:background">@color/colordarkgray</item>
    </style>
    <style name="MyTheme" parent="android:Theme.Light">
        <item name="android:windowTitleSize">45dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground</item>
    </style>

Activity 引用代码如下。

android:theme="@style/MyTheme" 

这样就可以实现自定义的效果了。


补充:上面说主题不继承 android:Theme.Light 会报错。查了一下是标题栏冲突的原因。
    我们只需要在继承的主题里添加如下属性就可以了。
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">false</item>
这样我们就可以继承别的主题使用一些高大上的效果了。如 Theme.AppCompat主题等。

有朋友问我,显示这个标题的时候同时也显示了系统自带的标题是什么原因,查看代码后发现他的Activity继承了AppCompatActivity 这里我们应继承Activity 就好了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值