Android 如何在另一个布局中添加布局?
我需要在一个布局中保留我的每个视图,以便我可以一次移动所有视图。。。但是当我试着把它们都放在一个应该放在另一个主布局中的布局中时,它们是看不见的。我做错了什么 我发现称为leftLayout的布局无法看到。我做了不同颜色的布局,它向我展示了主菜单。我试过了,但效果不太好 我的设计页面如下所示: 它应该是这样的,但我编译时只看到白色窗口 以下是我的Xml代码:Android 如何在另一个布局中添加布局?,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我需要在一个布局中保留我的每个视图,以便我可以一次移动所有视图。。。但是当我试着把它们都放在一个应该放在另一个主布局中的布局中时,它们是看不见的。我做错了什么 我发现称为leftLayout的布局无法看到。我做了不同颜色的布局,它向我展示了主菜单。我试过了,但效果不太好 我的设计页面如下所示: 它应该是这样的,但我编译时只看到白色窗口 以下是我的Xml代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmln
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/leftLayout"
android:addStatesFromChildren="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="70dp"
android:orientation="vertical" >
<TextView
android:id="@+id/leftMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:enabled="true"
android:text="Left Menu"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<ListView
android:id="@+id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_marginTop="70dp"
android:enabled="true" >
</ListView>
</RelativeLayout>
</RelativeLayout>
解决了在设置边距时,我发现我只是在java代码中犯了一个错误。因此,这里的代码是正确的。您使用的是相对布局,但尚未指定子视图相对于彼此的位置
在相对布局中,您必须指定此项,请参见此处:在相对布局中,您始终需要指定每个子项相对于父项的位置或子项之间的位置。因为您已经指定了布局,所以所有布局都将一个一个地复制。您可以使用子对齐选项(如“上方”、“下方”w.r.t)来调整其他布局。 下面的代码看起来像你想要的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/leftLayout"
android:addStatesFromChildren="true"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_alignleft="@+id/menu" >
<TextView
android:id="@+id/leftMenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:enabled="true"
android:text="Left Menu"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<ListView
android:id="@+id/list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:enabled="true"
android:layout_marginTop="50dp"
android:layout_weight="1"
android:layout_below="@+id/leftMenu" >
</ListView>
</RelativeLayout>
</RelativeLayout>
您尝试过线性布局吗
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<LinearLayout
android:id="@+id/leftLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="70dp"
android:orientation="vertical" >
<TextView
android:id="@+id/leftMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:enabled="true"
android:text="Left Menu"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:enabled="true" >
</ListView>
</LinearLayout>
</LinearLayout>
尝试将“android:layout_”置于layout leftMenu=“@+id/menu”之上,但无效:/I我看到了”菜单“是您的父相关布局,涵盖屏幕的宽度和高度。你怎么能在这上面添加一些东西呢!谢谢,但我发现我只是在java代码中犯了一个错误,设置了边距。代码没问题,你的问题不太清楚。将“子对象”更改为“线性”。并硬编码列表视图高度一次并检查它。不起作用。我发现称为leftLayout的布局无法看到。我做了不同颜色的布局,它向我展示了主菜单。