Android 定位具有多个包含的布局
为什么:Android 定位具有多个包含的布局,android,layout,Android,Layout,为什么: <RelativeLayout 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" tools:context=".MainActivity" &
<RelativeLayout 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"
tools:context=".MainActivity" >
<include
layout="@layout/myCustomTitleBar"
android:id="@+id/titlebar" />
<include
layout="@layout/myCustomToolBar"
android:id="@+id/toolbar"
android:layout_below="@id/titlebar" />
<com.myname.myapp.MyCustomView
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
/>
</RelativeLayout>
没有工作?两个
include
s相互堆叠/重叠。(下面的布局_
不起作用)include
s--myCustomTitleBar
和myCustomToolBar
--都是RelativeLayout
s.我做错什么了吗 预期结果:
尝试将android:layout\u alignParentTop=“true”添加到标题栏
<RelativeLayout 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"
tools:context=".MainActivity" >
<include
layout="@layout/myCustomTitleBar"
android:id="@+id/titlebar"
android:layout_alignParentTop="true" />
<include
layout="@layout/myCustomToolBar"
android:id="@+id/toolbar"
android:layout_below="@id/titlebar" />
<com.myname.myapp.MyCustomView
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
/>
</RelativeLayout>
尝试将android:layout\u alignParentTop=“true”添加到标题栏
<RelativeLayout 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"
tools:context=".MainActivity" >
<include
layout="@layout/myCustomTitleBar"
android:id="@+id/titlebar"
android:layout_alignParentTop="true" />
<include
layout="@layout/myCustomToolBar"
android:id="@+id/toolbar"
android:layout_below="@id/titlebar" />
<com.myname.myapp.MyCustomView
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
/>
</RelativeLayout>
根据你的应用程序草图,你不应该使用
RelativeLayout
作为根用户RelativeLayout
正是用于覆盖项目的。您必须使用LinearLayout
。当然,LinearLayout组的每个项目都必须具有适当的layout\u width/layout\u height
值
根目录:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/relative_sample_a"/>
<include layout="@layout/relative_sample_b"/>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#991022"/>
</LinearLayout>
相对样本a.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AACC33">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="ButtonA"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:text="ButtonB"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#DDBB00">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_centerInParent="true"
android:text="ButtonC"/>
</RelativeLayout>
相对样本.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AACC33">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="ButtonA"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:text="ButtonB"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#DDBB00">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_centerInParent="true"
android:text="ButtonC"/>
</RelativeLayout>
根据你的应用程序草图,你不应该使用
RelativeLayout
作为根用户RelativeLayout
正是用于覆盖项目的。您必须使用LinearLayout
。当然,LinearLayout组的每个项目都必须具有适当的layout\u width/layout\u height
值
根目录:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/relative_sample_a"/>
<include layout="@layout/relative_sample_b"/>
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#991022"/>
</LinearLayout>
相对样本a.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AACC33">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="ButtonA"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:text="ButtonB"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#DDBB00">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_centerInParent="true"
android:text="ButtonC"/>
</RelativeLayout>
相对样本.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#AACC33">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="ButtonA"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_alignParentRight="true"
android:text="ButtonB"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#DDBB00">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_centerInParent="true"
android:text="ButtonC"/>
</RelativeLayout>