include导入布局后原有的控件不可见的解决方法

在Android开发中,遇到通过include导入布局后原有控件不可见的问题,原因是title_layout的height设置为match_parent。修改为wrap_content后,由于edittext和button的width使用权重且orientation为vertical,导致它们仍然不可见。解决方案是将控件的width改为wrap_content,height设为0dp,并使用weight属性,或者包裹在一个horizontal的LinearLayout中,以实现正确显示。

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

原activity布局文件main_activity.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="match_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/edittext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"         
    />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"        
        android:text="button1" 
        /> 
</LinearLayout>


效果图如下


这里写图片描述


然后做了一个简单的标题栏title_layout.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="match_parent"
    android:orientation="horizontal" >

    <Button 
        android:id="@+id/btn_back"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="返回"
        />

    <TextView 
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="this is title"
        android:gravity="center"
        />

    <Button 
        android:id="@+id/btn_edit"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="编辑"
        />
</LinearLayout>

效果图如下

这里写图片描述


在main_activity.xml里通过include引入title_layout布局

<include layout="@layout/item_title"/>


结果运行出来是这样

这里写图片描述

检查了一下,发现title_layout布局里将android:layout_height这个属性设为了match_parent 。
这导致title_layout填满了整个屏幕
修改为wrap_content之后效果还是这样

这里写图片描述


这时我突然想起来,main_acitvity布局里原有的edittext和button都是以width属性作为权重比的:

android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_weight=”1”

但是main_activity布局的orientation属性是vertical,也就是说edittext和button是一上一下排列的,所以此时这两个控件都不可见了

将这两个控件的属性改为如下

android:layout_width=”wrap_content”
android:layout_height=”0dp”
android:layout_weight=”1”

效果如下

这里写图片描述


问题解决了~~~这里控件虽然显示出来了,但是不是我们想要的显示方式,这里可以在用一个orientation属性为horizontal的LinearLayout布局将这两个控件包起来

main_activity.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" >

 <include layout="@layout/item_title"/>
 <LinearLayout 
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     >
    <EditText
        android:id="@+id/edittext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"         
    />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"        
        android:text="button1" 
        />

</LinearLayout>

</LinearLayout>

最终效果如下

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值