Android 安卓儿童在相对论中身高相同
我想要在一个相对的窗口内有两个高度相同的视图,一个低于另一个。 我不能使用LinearLayout的重量,所以我必须想出另一种方法来实现 根据我在谷歌上搜索和阅读的内容,我找到了一个解决方案,在父视图中使用锚定视图中心水平,并将两个子视图一个放在锚定视图的上方,一个放在锚定视图的下方,但它似乎不起作用 到目前为止,我的代码是:Android 安卓儿童在相对论中身高相同,android,android-layout,Android,Android Layout,我想要在一个相对的窗口内有两个高度相同的视图,一个低于另一个。 我不能使用LinearLayout的重量,所以我必须想出另一种方法来实现 根据我在谷歌上搜索和阅读的内容,我找到了一个解决方案,在父视图中使用锚定视图中心水平,并将两个子视图一个放在锚定视图的上方,一个放在锚定视图的下方,但它似乎不起作用 到目前为止,我的代码是: <RelativeLayout android:layout_width="match_parent" android:layout_hei
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:gravity="top" >
<View android:id="@+id/anchor"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#000"/>
<RelativeLayout
android:id="@+id/toplayout"
android:background="#990000"
android:layout_above="@+id/anchor"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="@+id/bottomlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/anchor">
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="218dp"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
我的代码是否有错误,或者我试图解决它的方法从一开始就是错误的?您的方法是正确的,但是通过锚定视图,您将其布局设置为中心水平,但它应该是中心垂直。此外,还可以使用“顶部和底部对齐”来匹配视图的边
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="top" >
<RelativeLayout
android:id="@+id/toplayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#990000"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/anchor"
android:layout_above="@+id/anchor">
</RelativeLayout>
<View android:id="@+id/anchor"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:background="#000"/>
<RelativeLayout
android:id="@+id/bottomlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/anchor"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/anchor" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="218dp"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>
您的方法是正确的,但是通过锚定视图,您将其布局设置为中心水平,但它应该是中心垂直。此外,还可以使用“顶部和底部对齐”来匹配视图的边
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="top" >
<RelativeLayout
android:id="@+id/toplayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#990000"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/anchor"
android:layout_above="@+id/anchor">
</RelativeLayout>
<View android:id="@+id/anchor"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:background="#000"/>
<RelativeLayout
android:id="@+id/bottomlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/anchor"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/anchor" >
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="218dp"
android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>