zoukankan      html  css  js  c++  java
  • Android-----布局复用

       Android应用发展得越来越快,布局也随之变得越来越复杂,在Android中布局文件也可以像Java代码一样具有复用性,使得代码可以变得清晰。

       Android布局代码实现复用性主要是利用include和merge这两个关键字

    1、include
    Android提供了在一个布局中包含另一个布局的简单方法:<include>标签
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <include layout="@layout/sub_layout" />
    </LinearLayout>
    sub_layout.xml
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView 
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Hello,Android!" />
    </LinearLayout>
    2、merge
    如果经常使用<include>标签,布局会变得因嵌套过多从而导致UI绘图变慢。<merge>标签可以用来解决这个问题。
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertivcal">
    
        <include layout="@layout/merge_text">
    </LinearLayout
    <merge>
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello merge" />
    </merge>
    3、ViewStub
       尽管<include>标签能够很容易把UI分离成可复用的组件,但会发现包含的布局很少会被用到。类似于进度和错误蓝的布局是可用的,但是他们在正常的使用过程中并不会出现。当被包含进布局以后,这些UI元素仍然是被扩张的,他们会占用CPU周期和内存,尽管不会被展示出来。
       ViewStub类是一个不会占据布局控件的不可见的视图。像<include>标签一样,ViewStub引用一个将会被添加到UI上的外部布局。与<include>标签不同,被引用的布局不会被扩张,直到发送特定的请求他才会扩张。有了ViewStub,布局的可选部分只会在需要他们的时候才可用,否则他们就不可见,这将会加快UI的绘制速度。
    <ViewStub
        android:id="+id/view_stub"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inflatedId="@+id/sub"
        android:layout="@layout/sub" />
    调用ViewStub的方法
    
    //Inflate by changing the visibility of the ViewStub
    ((ViewStub)findViewById(R.id.view_stub)).setVisibility(View.VISIBLE);
    //or by calling inflate
    View v  = ((ViewStub)findViewById(R.id.view_stub)).inflate();
  • 相关阅读:
    CF 142B Tprimes
    CF 231A Team
    poj 2001 Shortest Prefixes ——字典树入门
    hdu 1039 Easier Done Than Said?
    poj 2528 Mayor's posters
    hdu 1061 Rightmost Digit
    poj 2503 Babelfish
    CF271 A. Beautiful Year
    poj 2752
    CF271 B. Prime Matrix
  • 原文地址:https://www.cnblogs.com/vijay/p/3533155.html
Copyright © 2011-2022 走看看