Android 在水平滚动视图的RelativeLayout中布局相对定位点不';我不能如愿工作

Android 在水平滚动视图的RelativeLayout中布局相对定位点不';我不能如愿工作,android,layout,android-relativelayout,horizontalscrollview,Android,Layout,Android Relativelayout,Horizontalscrollview,我有一个水平滚动视图,其中有一个相对视图包含三个视图s。一个在父视图中居中,另两个应作为定位相对于居中视图放置。这并没有给出预期的结果。但是当RelativeLayout未放在滚动视图中时,它会放在滚动视图中 <?xml version="1.0" encoding="utf-8"?> <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layo

我有一个
水平滚动视图
,其中有一个
相对视图
包含三个
视图
s。一个在父视图中居中,另两个应作为定位相对于居中视图放置。这并没有给出预期的结果。但是当
RelativeLayout
未放在滚动视图中时,它会放在滚动视图中

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <RelativeLayout
        android:id="@+id/container"
        android:layout_width="600dp"
        android:layout_height="200dp"
        android:background="#eeeeee">
        <View
            android:id="@+id/center"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:background="#ff0000" />
        <View
            android:id="@+id/left"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/center"
            android:background="#00ff00" />
        <View
            android:id="@+id/right"
            android:layout_width="50dp"
            android:layout_height="match_parent"
            android:layout_toRightOf="@+id/center"
            android:background="#0000ff" />
    </RelativeLayout>
</HorizontalScrollView>

这种并排排列只是为了提供一个简单的展示。实际上,将添加边距(正边距和负边距)以在视图之间形成间隙或重叠,因此
线性布局将不起作用


会议上的说明表明,这方面存在一个已知的问题。有解决办法吗?

作为一名读者,我还不能发表评论,所以这里有一个提示作为答案

这可能会有所帮助 您是否尝试添加(在中):

或 android:layout\u width=“fill\u parent”

取决于方法

不幸的是,大多数示例都使用LinearLayout

浏览:
:)

需要将
相对视图
包装在
线性布局中
才能强制布局计算以正确的顺序定位视图

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="600dp"
            android:layout_height="200dp"
            android:background="#eeeeee">
            <View
                android:id="@+id/center"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:background="#ff0000" />
            <View
                android:id="@+id/left"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:layout_toLeftOf="@+id/center"
                android:background="#00ff00" />
            <View
                android:id="@+id/right"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:layout_toRightOf="@+id/center"
                android:background="#0000ff" />
        </RelativeLayout>
    </LinearLayout>
</HorizontalScrollView>


如果使用LinearLayout更改RelativeLayout,会发生什么?LinearLayout提供了预期的行为,但这不允许我更改z索引,使绿色和蓝色视图可以具有负边距,并与红色视图重叠。把绿色带到最前面,直到最后。
android:layout_width="wrap_content"
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RelativeLayout
            android:id="@+id/container"
            android:layout_width="600dp"
            android:layout_height="200dp"
            android:background="#eeeeee">
            <View
                android:id="@+id/center"
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:layout_centerInParent="true"
                android:background="#ff0000" />
            <View
                android:id="@+id/left"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:layout_toLeftOf="@+id/center"
                android:background="#00ff00" />
            <View
                android:id="@+id/right"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:layout_toRightOf="@+id/center"
                android:background="#0000ff" />
        </RelativeLayout>
    </LinearLayout>
</HorizontalScrollView>