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" /> <!--2. 存放四个Fragment--> <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