android布局 include,Android布局优化之include标签详解

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

原CSDN博客已弃用,文章会逐渐迁移过来。

一、include标签(布局重用)

1、说明:该标签的目的是解决重复定义布局的问题而诞生的,提高代码的复用。

2、使用方法:

android:layout_width="match_parent"

android:layout_height="wrap_content"

layout="@layout/my_title_layout" />

my_title_layout.xml<?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent" android:id="@+id/my_title_parent_id"

android:layout_height="wrap_content" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@mipmap/ic_launcher" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerVertical="true"

android:layout_marginLeft="20dp"

android:layout_toRightOf="@+id/back_btn"

android:gravity="center"

android:text="我的title"

android:textSize="18sp" />

include布局文件:<?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

layout="@layout/my_title_layout" />

4、注意事项:

子控件抛出空指针的情况

include标签若指定了ID属性,而你的layout也定义了ID,则你的layout的ID会被覆盖。如果findViewById()查找layout的Id来查找子控件,会出现这种情况。

解决:不用上边的方法。可以直接查找子控件的id。或者查找include标签的id来查找子控件。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值