zoukankan      html  css  js  c++  java
  • 使用include重用布局

    尽管Android 支持各种小部件,来提供小且可以重用的交互元素,你可能还需要更大的,要求一个专门布局的重用组件。为了高效的重用整个布局,你能使用标签在当前的布局中嵌入别的布局。

    重用布局功能特别强大,因为它允许你创建可重用的复杂布局。例如,一个yes/no按钮面板,或者自定义带有描述字符串的滚动条。也就是说,在你的应用中任何跨越多个布局的相同元素都能被提取,单独管理,然后被包含在每个布局中。所以当你通过编写一个自定义View创建独特的UI组件的时候,通过重用一个布局实现它或许更容易 。

    创建一个重用布局

    如果你已经知道了你想重用的布局,创建一个新的XML文件定义它。例如,这里有一个自G-Kenya codelag的布局,它定义了标题栏,被包含在每个Activity中(titlebar.xml)。

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"   
        android:layout_width=”match_parent”   
        android:layout_height="wrap_content"   
        android:background="@color/titlebar_bg">   
       
        <ImageView android:layout_width="wrap_content"   
                   android:layout_height="wrap_content"    
                   android:src="@drawable/gafricalogo" />   
    </FrameLayout>  
    

    在每个添加这个布局的布局中,根View决定了你想如何显示它。

    使用标识

    在你想添加这个重用组件的布局中,添加标签。例如,下面的布局来自G-Kenya codelab,它包含了上面提到的标题栏:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
        android:orientation="vertical"    
        android:layout_width=”match_parent”   
        android:layout_height=”match_parent”   
        android:background="@color/app_bg"   
        android:gravity="center_horizontal">   
        <include layout="@layout/titlebar"/>   
        <TextView android:layout_width=”match_parent”   
                  android:layout_height="wrap_content"   
                  android:text="@string/hello"   
                  android:padding="10dp" />   
        …   
    </LinearLayout>   
    

    通过在标签中指定它们,你能覆盖被包含布局根视图的所有布局参数(任何android:layout_*属性)。例如:

    <include android:id=”@+id/news_title”   
             android:layout_width=”match_parent”   
             android:layout_height=”match_parent”   
             layout=”@layout/title”/>   
    

    不管怎样,如果你在使用标签时覆盖布局属性,为了对别的布局属性生效,你必须都覆盖android:layout_height和android:layout_width属性。
    使用标识

    当一个布局包含另一个时,标签帮助消除在你的视图结构中冗余的视图组。例如,如果你的主布局是一个竖直的LinearLayout,在其中两个连续的视图可以在多个布局中重用,那么你使用的这两个重用的视图需要自己的根视图。无论如何,使用其它LinearLayout作为重用布局的根节点,会导致一个竖直LinearLayout嵌套一个竖直LinearLayout中。这个嵌入的LinearLayout不仅没到启到真正的作用,反而减缓了你的UI性能。

    为了避免包含这样一个多余的视图组,你能使用元素替代这个重用布局的的根视图。例如:

    <merge xmlns:android="http://schemas.android.com/apk/res/android">   
        <Button   
            android:layout_width="fill_parent"    
            android:layout_height="wrap_content"   
            android:text="@string/add"/>   
        <Button   
            android:layout_width="fill_parent"    
            android:layout_height="wrap_content"   
            android:text="@string/delete"/>   
    </merge>   
    

    现在,当你在其它的布局中包含这个布局的时候(使用标签),系统不顾这个元素并直接在这个布局中放置这两个按钮,取代标签。

  • 相关阅读:
    ArrayList removeRange方法分析
    LinkedHashMap源码分析(基于JDK1.6)
    LinkedList原码分析(基于JDK1.6)
    TreeMap源码分析——深入分析(基于JDK1.6)
    51NOD 2072 装箱问题 背包问题 01 背包 DP 动态规划
    51 NOD 1049 最大子段和 动态规划 模板 板子 DP
    51NOD 1006 最长公共子序列 Lcs 动态规划 DP 模板题 板子
    8月20日 训练日记
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/xl-phoenix/p/8056015.html
Copyright © 2011-2022 走看看