Android自定义标题栏

新建Activity的时候可以继承父类AppCompatActivity和Activity

但是AppCompatActivity默认是添加标题栏的,所以如果需要自定义标题栏可以设置一下

1.想要这些活动没有标题栏,必须在manifest.xml文件中设置Application的主题为:

android:theme="@android:style/Theme.NoTitleBar"

2.AppCompatActivity这个非要标题栏才不会出错,那么就需要在oncreate()中设置它是否显示标题栏

getSupportActionBar().hide();

此时manifest.xml就不需要上一步的修改了

3.自定义标题栏

在布局文件中使用自定义组件设置标题栏

这是我写的一个简单的标题栏

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RelativeLayout
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="30dp">

        <TextView
            android:id="@+id/barText"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:layout_centerVertical="true"
            android:textSize="28sp"
            android:layout_marginLeft="5dp"
            android:text="首页"
            />

        <ImageView
            android:id="@+id/barAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_menu_add"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_centerVertical="true"
            />

        <ImageView
            android:id="@+id/barSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_menu_search"
            android:layout_toLeftOf="@+id/barAdd"
            android:layout_marginRight="20dp"
            android:layout_centerVertical="true"
            />

    </RelativeLayout>

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</LinearLayout>

最后贴一下出错的处理方案:

https://blog.csdn.net/jason0539/article/details/12979005

https://blog.csdn.net/wzhidev/article/details/64129732

还有一个设计的参考:

https://www.cnblogs.com/cxyc/p/5377873.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值