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

来源:互联网 发布:hp1505n网络打印设置 编辑:程序博客网 时间:2024/05/22 05:26

原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>

最终效果如下

这里写图片描述

0 0
原创粉丝点击