Android中xml布局文件中使用include引入布局进行复用(解决使用include布局重叠,颜色设置无效问题)

编写不易,如有转载,请声明出处:http://blog.csdn.net/zxc514257857/article/details/72614683

使用include引入布局的作用

  提取重复的布局代码,方便进行复用

如何使用

  比如我们想要在线性布局中创建三块需要复用的布局headview、centerview、buttomview(用Button简单代替)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <Button
        android:id="@+id/headview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/centerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/buttomview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

  代码写出来了,但其他布局如果也要使用centerview,就需要将布局拷贝过去,导致代码冗余,但使用include引入centerview就可以让其他布局进行复用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include layout="@layout/headview"
             android:id="@+id/headview"/>

    <include layout="@layout/centerview"
             android:id="@+id/centerview"/>

    <include layout="@layout/buttomview"
             android:id="@+id/buttomview"/>

</LinearLayout>

  于是,我们就采用如下方法引入headview、centerview、buttomview,但是我们会发现,这三个布局重叠到一起去了,并没有像我们想象的上下堆叠排列

include使用注意事项

  1,必须同时重载layout_width和layout_height属性,其他的属性才会起作用,否则会被忽略掉。
  2,include里面设置背景颜色是不生效的,必须在include的布局里面写,include里面设置位置及大小信息,然后直接在include的布局里面设置match_parent即可

  于是include引入布局代码正确写法如下:

//(layout)activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include layout="@layout/headview"
             android:id="@+id/headview"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>

    <include layout="@layout/centerview"
             android:id="@+id/centerview"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>

    <include layout="@layout/buttomview"
             android:id="@+id/buttomview"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)headview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/headview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)centerview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/centerview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
-------------------------------------------------------------------
//(layout)buttomview
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
        android:id="@+id/buttomview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

----------因本人才疏学浅,如博客或Demo中有错误的地方请大家随意指出,与大家一起讨论,共同进步,谢谢!----------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DreamBackTo

感谢各位金主大大(* _ *)

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值