Android--使用include调用布局

本文介绍了如何在Android开发中利用include标签提高布局复用效率,通过实例展示了如何解决include引入的布局不按预期显示的问题,特别是foot布局无法始终置底的调整方法,包括将LinearLayout转换为RelativeLayout,以及使用android:layout_alignParentTop和android:layout_alignParentBottom属性确保组件定位。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天写界面时,为了提高效率,避免重复,将界面中的title和foot分别写成两个xml布局文件,只需在需要添加的xml中填写2句include语句即可。

但是刚开始遇到一个问题,在xml文件头添加 并没有问题,
但是在尾部添加发现 foot并没有一直在底部,而是紧随着上面的内容。
为了让foot置底,只能通过调节中间控件的android:layout_height属性。

解决办法:
将原LinearLayout改成RelativeLayout后,再分别在title和foot文件中添加 android:layout_alignParentTop=”true” 和 android:layout_alignParentBottom=”true” 来实现

一个简单的例子:
title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_alignParentTop="true"  
    android:background="@color/title_color">

    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="72dp"
        android:gravity="center"

        android:textSize="24sp"
        android:textColor="@color/white"
      />
</LinearLayout>

foot.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_alignParentBottom="true" 
    android:background="@color/foot_color">
    <View 
       android:layout_width="match_parent"
       android:layout_height="3dp" 
       android:background="@color/foot_line"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/foot_hint"
            android:layout_width="match_parent"
            android:layout_height="95dp"
            android:paddingLeft="22dp"
            android:paddingTop="22dp"

            android:textColor="@color/foot_word_color"
            android:textSize="23sp" />

         <LinearLayout 
              android:layout_width="match_parent"
              android:layout_height="90dp"
              android:layout_marginTop="20dp"
              android:orientation="horizontal"          
            >

             <Button
                 style="?android:attr/borderlessButtonStyle"
                 android:id="@+id/btn_stop"
                 android:layout_width="135dp"
                 android:layout_height="46dp"
                 android:layout_marginLeft="48dp"
                 android:background="@drawable/bnt_selector"
                 android:gravity="center"
                 android:text="@string/stop"
                 android:textColor="@color/white"
                 android:textSize="21sp" />

              <Button 
                style="?android:attr/borderlessButtonStyle"
                android:id="@+id/btn_next"
                android:layout_width="135dp"
                android:layout_height="46dp"                
                android:gravity="center"
                android:text="@string/next"
                android:textColor="@color/white"
                android:textSize="21sp"
                android:background="@drawable/bnt_selector"     
                android:layout_marginLeft="72dp"                                 
                 />                     
         </LinearLayout>     
    </LinearLayout>
</LinearLayout>

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@color/white">
     <!-- 标题 -->
    <include 
        android:id="@+id/include_title"            
        layout="@layout/title" >       
    </include>
   <!-- 尾文件 -->
    <include
        android:id="@+id/include_foot"   
        layout="@layout/foot" >
    </include>

    <LinearLayout 
      android:layout_width="match_parent"
      android:layout_height="match_parent"      
      android:layout_below="@id/include_title"
      android:layout_above="@id/include_foot"
    >
       <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"        
         android:src="@drawable/sound_image" />      
    </LinearLayout>

</RelativeLayout>

效果图:
这里写图片描述

通过android:layout_below=”@id/include_title” 和 android:layout_above=”@id/include_foot” 将图片填充中间位置,从而不需要通过android:layout_height来调节让foot安置在底部。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值