抽象布局 include merge ViewStub 自定义标题

1.include 布局重用

<include>标签唯一需要的属性是layout属性,指定需要包
含的布局文件。可以定义android:id和android:layout_*属
性来覆盖被引入布局根节点的对应属性值。


include 引入可以达到重用布局的效果 但是不设置位置信息,比较难看。  

要想设置include标签的布局,必须设置宽高。  

如果id起冲突了 那么以本layout的id为响应id,include导入layout的id会被覆盖掉 。

在Activity的布局中的include

<include
    layout="@layout/footlayout"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content" />
                        新建一个xml文件添加布局作为布局根节点

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="点击" />
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20sp"
        android:layout_toRightOf="@+id/button"/>
</RelativeLayout>


2.merge减少视图层级

<merge/>标签在UI的结构优化中起着非常重要的作用,它可以删减多余的层级,优化UI。<merge/>多用于替换FrameLayout或者当一个布局包含另一个时,<merge/>标签消除视图层次结构中多余的视图组。例如你的主布局文件是垂直布局,引入了一个垂直布局的include,这是如果include布局使用的LinearLayout就没意义了,使用的话反而减慢你的UI表现。这时可以使用<merge/>标签优化。


      include部分代码

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20sp"
        android:layout_toRightOf="@+id/button"
        android:layout_alignParentBottom="true" />
</merge>
            在Activity的布局中的include

<include
    layout="@layout/footlayout"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_width="wrap_content" />

3. ViewStub

<ViewStub />标签最大的优点是当你需要时才会加载,使用他并不会影响UI初始化时的性能。各种不常用的布局想进度条、显示错误消息等可以使用<ViewStub />标签,以减少内存使用量,加快渲染速度。<ViewStub />是一个不可见的,大小为0View。

  注意:ViewStub目前不支持merge标签

<ViewStub
    android:id="@+id/viewStub"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout="@layout/footlayout"/>

4.自定义标题

   使用自定义标题栏遵循以下步骤: 
       1、自定义标题栏布局
       2、在Activity中使用该布局 

//这一行代码必须加在setContentView之前
//取消系统的标题栏,使用自定义的标题栏
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
//设置当前Activity使用自定义标题栏,指定当前的自定义标题栏为创建的标题栏布局文件
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_layout);

       3、调整style使该布局和标题栏契合 

<!--在这里创建自己的style,用于系统主题的使用-->
name : 指定样式的名字,parent :指定该样式所继承的父样式
<style name="myTitleBar" parent="android:Theme">
    <!--设置原始标题栏的高度-->
    <item name="android:windowTitleSize">50dp</item>
    <!--设置新的标题栏完全延伸到原始标题栏的左右两则-->
    <item name="android:padding">0dp</item>
</style>

       4、让Activity使用该样式

             在Manifest中添加Theme的style

             方法1:在每个Activity中添加

<activity android:name=".MainActivity"
    android:theme="@style/myTitle"
    >

                                   方法2:  在applicton的Theme中添加style即可,

                         不必再在每 一个Activity中添加

android:theme="@style/myTitle">


















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值