抽象布局

在Android的性能优化时,可以使用使用抽象布局标签(include, ViewStub, merge)、去除不必要的嵌套和View节点、减少不必要的inflate及其他Layout。

**include**include标签常用于将布局中的公共部分提取出来是其他layout公用,以实现布局的模块化,这给布局编写提供了便利。要想设置include 标签的布局,必须先设置宽高。 当在用include的时候首先要写一个 xml 布局 (在下建的xml 的名字为:foot.XML)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.coderqi.android2lesson_07_interfacelayout.MainActivity"
    android:orientation="vertical">

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <!-- include 引用可以达到重用布局的效果
     但是  如果不设置位置信息是比较难看的
     要想设置include 标签的布局,必须先设置宽高
     如果id起冲突了?
     那么以本layout的id为响应id,include导入的layout的id会陪覆盖掉-->

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

merge:

merge标签可用于两种典型情况 <merge/>多用于替换

FrameLayout或者当一个布局包含另

一个时,<merge/>标签消除视图层次结构中多余的视图

组。例如你的主布局文件是相对布局,引入了一个相对布

局的include,这时如果include布局使 RelativeLayout

就没意义了,使用的话反而减慢你的UI表现。这时可以


使 用<merge/>标签优化。

<merge/>标签在UI的结构优化中起着非常重要的作用,

可以删减多余的层级,优化UI。





viewstub:

viewstub引入的布局默认不会扩张,即既
不会占用显示也不会占用位置,从而在解析layout时节省 cpu和内存。

viewstub常用来引入那些默认不会显示,只在特殊情况下 显示的布局,如进度布局、网络失败显示的刷新布局、信 息出错出现的提示布局等。

viewstub 不能 使 用<merge/>标签优化。

要想让viewstub 引入的布局显示出来要调用 .inflate() 方法
((ViewStub) findViewById(R.id.viewStub)).inflate();

点击显示,再点击不显示
if (view1.getVisibility() == View.VISIBLE){
    view1.setVisibility(View.INVISIBLE);
}else{
    view1.setVisibility(View.VISIBLE);
}

 自定义标题栏:

自定义标题栏步骤:
1 自定义标题栏布局文件(也就是创建一个布局文件)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:background="@drawable/qq"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="自定义标题栏"
        android:textSize="20sp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>


2 在对应的activity当中指定使用自定义的  标题栏(布局)
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //这行代码必须加在setContent之前
    //取消系统的标题栏(使用自定义标题栏)
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_main);
    //设置使用当前activity使用自定义标题栏,指定使用自定义标题栏为刚才创建的布局文件
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.title_layout);

3 将自定义标题和主题进行融合,创建一个自己的style
(调整style使布局和标题栏契合)
<!-- 创建自己的style ,用于系统主题的使用 -->
<style name="myTitleBar" parent="android:Theme">
    //设置原始标题栏的高度
    <item name="android:windowTitleSize">50dp</item>
    //设置新的标题栏完全延伸到对齐到原始标题栏的左右两侧
    <item name="android:padding">0dp</item>
</style>

4 配置清单文件,设置使用自定义主题



注意:mainactivity要继承与activity


改成:










































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值