zoukankan      html  css  js  c++  java
  • 关于Android布局优化的代码使用

    1.  include标签:

          include标签的作用是在一个布局文件中导入另一个布局文件。在开发中经常会有多个页面同时拥有一部分相同的布局,这个时候如果每个布局都把那个部分的代码写一遍就会使得代码重复,浪费了资源。使用include就可以简单的导入相同部分的布局。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="欢迎您使用这款app"
    android:textSize="16dp"
    android:gravity="center"/>
    </LinearLayout>

    以上代码是一个多页面通用的布局,给它命名为include.xml,接下来在主页面activity_main.xml中引用它:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <include layout="@layout/include"/> //使用include引用了子布局

    </LinearLayout>
    于是在主页面中便看到了include页面中的内容。

    2. merge标签:
    merge标签的作用是减少布局嵌套的层次。比如刚才那个例子中,父布局与子布局都是使用的线性布局LinearLayout,这样在代码中
    等于重复写了两次的布局声明,产生了不必要的代码,这个时候就可以使用merge标签。
    <?xml version="1.0" encoding="utf-8"?>
    <merge xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="欢迎您使用这款app"
    android:textSize="16dp"
    android:gravity="center"/>
    </merge>
    以上是修改过后的include.xml。在父布局中导入了这个页面后,这个页面就得到了父布局的LinearLayout布局,所以显示
    效果和之前的代码相比没有任何区别,但是精简了代码,节省了系统资源。
    3.Viewstub标签:
    Viewstub标签的作用是让布局在被加载的时候才会去占用资源,而INVISIBLE状态(不可见状态)下布局不会被绘制
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ViewStub
    android:id="@+id/include1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout="@layout/include"/>
    </LinearLayout>
    以上是一个Viewstub的例子,运行程序后会发现Viewstub里的布局include.xml并没有被显示出来,说明Viewstub默认状态
    下是会隐藏内容的,然后我们将它改为可视状态:
    package com.example.von.include;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    findViewById(R.id.include1).setVisibility(View.VISIBLE); //利用这个方法将include1改为可视状态
    }
    }
    这时再次运行程序,会发现include.xml的内容成功显示了出来。

  • 相关阅读:
    老友记实战,17上
    老友记实战,9下
    老友记实战,5下
    公共样式base.css
    单选框radio总结(获取值、设置默认选中值、样式)
    js tab切换
    HTTP状态码100、200、300、400、500、600的含义
    微信小程序事件绑定
    微信小程序获取手机验证码
    js滚动到指定位置导航栏固定顶部
  • 原文地址:https://www.cnblogs.com/vonzc/p/10332350.html
Copyright © 2011-2022 走看看