Android开发--自定义颜色渐变的标题栏简单Demo
16lz
2021-01-26
Android开发常用模版:
在Android的开发过程中,会常常遇到消除系统自带标题栏而自定义标题栏的情况,这里分享一个自定义颜色渐变的标题栏简单Demo
概述
先看一下效果图(我可能男生审美比较差。。。):
带按钮的:
不带按钮的:
实现过程
- 标题栏的引入:
这里给的实际例子是之前给的一个导航栏例子的添加标题栏的例子
链接:http://blog.csdn.net/qq_34861102/article/details/76737673
我们进行了一个show_main_title
的添加
<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/color_tab_1"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical"> <include android:id="@+id/show_main_title" layout="@layout/title_layout" /> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/white">FrameLayout> <TextView android:layout_marginTop="45dp" android:layout_height="wrap_content" android:layout_width="wrap_content" /> LinearLayout> ScrollView> <com.aurelhubert.ahbottomnavigation.AHBottomNavigation android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginTop="10dp" android:layout_gravity="bottom" android:layout_weight="1" app:selectedBackgroundVisible="true" />android.support.design.widget.CoordinatorLayout>
大家可以看到:
这里标题栏的实现是
<include android:id="@+id/show_main_title" layout="@layout/title_layout" />
也就是标题栏的实现
新建一个文件
title_layout.xml
在layout
中
代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="56dp" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="@drawable/barshape" android:orientation="horizontal"> <TextView android:id="@+id/title_text_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="首 页" android:textSize="20dp" android:textColor="@color/color_tab_2"/> <ImageView android:id="@+id/icon_back" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_marginRight="15dp" android:visibility="gone" app:srcCompat="@drawable/myphoto_05" />RelativeLayout>
你会发现,这个代码会报2个错误,一个是图片没有加入,加入即可,另外一个是
RelativeLayout
中的android:background
没有找到,这就是:渐变的实现
缺少一个
barshape.xml
文件同理:添加(在
drawable
中)<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <gradient android:startColor="#03b79a" android:endColor="#31205e" android:type="linear" />shape>
可以看到
shape
里面的属性是非常容易看清楚的
至此:一个自定义标题栏就实现了,如果想要对标题栏中标题内容有修改,可以直接:
- 标题栏的引入:
private TextView title_text_tv;private ImageView icon_back;icon_back = (ImageView) findViewById(R.id.icon_back);title_text_tv = (TextView) findViewById(R.id.title_text_tv);title_text_tv.setText("点评");
源代码参考:http://download.csdn.net/detail/qq_34861102/9922478
用文章中的xml文件替换其中activity_main.xml
即可
原文地址:http://blog.csdn.net/qq_34861102/article/details/76772717
更多相关文章
- android中使用代码混淆出现Conversion to Dalvik format failed
- 如何配置ubuntu服务器用于embedded-linux的开发
- Android(安卓)apk项目中添加ffmpeg编解码库
- Android通过lame进行音频格式转换
- Android(安卓)Activity之间跳转出现短暂黑屏的处理方法和intent.
- Android程序员指南(4)
- Android获得线性渐变某点的颜色
- android 自定义Button按钮显示样式
- android 购物车数量加减计算(几行代码实现效果)