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.xmllayout
      代码

     <?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

更多相关文章

  1. android中使用代码混淆出现Conversion to Dalvik format failed
  2. 如何配置ubuntu服务器用于embedded-linux的开发
  3. Android(安卓)apk项目中添加ffmpeg编解码库
  4. Android通过lame进行音频格式转换
  5. Android(安卓)Activity之间跳转出现短暂黑屏的处理方法和intent.
  6. Android程序员指南(4)
  7. Android获得线性渐变某点的颜色
  8. android 自定义Button按钮显示样式
  9. android 购物车数量加减计算(几行代码实现效果)

随机推荐

  1. Android(安卓)实现适配器中的子控件与Act
  2. Android绘制进阶之三:在位图上(Bitmap)绘
  3. andriod一个不错的启动菜单显示屏动画效
  4. Android(安卓)Studio 注解插件Android(安
  5. 代码设置RadioButton的高端技巧 - wavky
  6. Android(安卓)如何通过menu id来得到menu
  7. Android设置透明效果的三种方法
  8. 【Android】5.3 单选和复选
  9. Android使用SimpleAdapter
  10. android通过http上传文件(图片)