• Android include的使用


    如果在程序中多次用到一部分相同的布局,可以先将这部分布局定义为一个单独的XML,然后在需要的地方通过<include>引入,如下:

    main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <include
            android:id="@+id/cell1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="45dp"
            android:layout_marginTop="10dp"
            layout="@layout/item" />
    
        <include
            android:id="@+id/cell2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/cell1"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/cell1"
            layout="@layout/item" />
    
        <include
            android:id="@+id/cell3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/cell1"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/cell2"
            layout="@layout/item" />
    
    </RelativeLayout>

    item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="invisible" >
    
        <ImageView
            android:id="@+id/iv_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#000000"
            android:clickable="true"
            android:focusable="false" />
    
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/iv_img"
            android:layout_below="@+id/iv_img"
            android:textColor="#a17006"
            android:textSize="22dp"
            android:textStyle="bold" />
    
    </RelativeLayout>

    使用Android include时需要注意的是要指定宽高属性,要不可能会出现一些意想不到的效果,比如引用了三次,而界面上只显示了一个item。

  • 相关阅读:
    01 Windows编程——Hello World
    图像处理基础知识
    集成IDE anaconda
    Python中的正则表达式
    Introduction of Machine Learning
    Linux命令——diff、patch
    sed & awk 概述
    Linux行编辑器——ed
    Linux命令——w、who、whoami、lastlog、last
    【问题】统计系统上有多少个用户
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/4288735.html
Copyright © 2020-2023  润新知