Android 自定义标题栏

Activity 代码

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //声明 使用自定义标题栏,该声明 必须在 setContentView() 方法前
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_main);
        //使用自定义标题栏。
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
    }

custom_title.xml 是一个LinearLayout布局文件

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

    <ImageView
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:src="@drawable/custom"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_weight="1"
        android:textSize="18dp"
        android:text="MyMusic"
        android:gravity="center"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:textSize="18dp"
        android:layout_weight="1"
        android:text="推薦"
        android:gravity="center"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:textSize="18dp"
        android:layout_weight="1"
        android:text="發現"
        android:gravity="center"/>

</LinearLayout>

AndroidManifest.xml
application中设置theme 表示作用于 整个App
activity中设置theme表示 只作用于该 activity

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyCustomTheme" >
        <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>

AndoridManifest.xml 文件中 application标签的theme属性的值 原先为AppTheme,运行时出现以下错误并闪退
这里写图片描述

You cannot combine custom titles with other title features
不能将自定义标题和其他标题功能组合起来(意思为 可能存在有多个标题)

随后将theme 属性值 改为自定义的值 MyCustomTheme
还是出现这种错误,但没有闪退 有效果显示。
被这坑爹的错误 坑了一天,在我关掉AS重启后运行 竟然就没了。。
难道是 AS 严重缓存,期间 进行过多次Clean 还是报错。 。。蛋疼的AS

MyCustomTheme 在 res/values/style.xml 文件中

parent属性表示继承, 值为系统自带theme 。
只有设置了parent 属性的style 才会被认可为 Theme。
这点 可以在 layout布局文件编辑中查看
这里写图片描述

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="TitleBarBackground">
       <!--设置标题栏背景色-->
        <item name="android:background">#50323232 </item>
    </style>

    <style name="MyCustomTheme" parent="android:Theme">
        <item name="android:windowTitleBackgroundStyle">@style/TitleBarBackground</item> 
        <!--设置标题栏大小-->
        <item name="android:windowTitleSize">70dp</item>
    </style>
</resources>

运行效果:
这里写图片描述




另一种 “标题栏”:

有时需要设置标题栏 半透明显示,能够透过标题栏看到内容:如图效果
这里写图片描述
但在 style.xml 中 设置theme半透明 并不能达到这种效果。

其实达到这种效果比之前那种标题栏 实现起来要简单的多。
只需要在style.xml中 的AppTheme 继承 Theme.NoTitleBar 就可以。
没错 就是把标题栏去掉。然后在布局文件中设置一个“标题栏”就可以了
res/layout/activity_main.xml:

<!--设置背景图片, 更明显的显示透明效果-->
    android:background="@drawable/back"
    tools:context=".MainActivity">
<!-- 只是把之前写好的 custom_title.xml 加载进来。
   custom_title.xml中设置半透明颜色-->
    <include layout="@layout/custom_title"
       android:id="@+id/title"/>
    <TextView
        android:layout_below="@id/title"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

res/value/style.xml

<resources>
    <style name="AppTheme" parent="android:Theme.NoTitleBar">
    </style>
</resources>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值