Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将视图定位到同级视图的右侧';谁看孩子?Android XML_Java_Android_Android Layout - Fatal编程技术网

Java 如何将视图定位到同级视图的右侧';谁看孩子?Android XML

Java 如何将视图定位到同级视图的右侧';谁看孩子?Android XML,java,android,android-layout,Java,Android,Android Layout,假设我有以下XML <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" an

假设我有以下XML

    <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content">

        <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
            <RelativeLayout
                          android:id="@+id/child1"
                          android:layout_width="0dp"
                          android:layout_height="match_parent"
                          android:layout_weight="2">

                <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="15sp"/>

            </RelativeLayout>

            <RelativeLayout
                        android:id="@+id/child2"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="2">
            </RelativeLayout>

        </LinearLayout>

        <View 
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:layout_toEndOf="@+id/child1"/> // CAN'T DO THIS need work around

    </RelativeLayout>

//做不到这一点,需要变通
我有类似的东西。我有一个
线性布局
,在
RelativeLayout
中包含许多子项,我有一个与child1和child2重叠的
视图。但我希望它只和孩子2重叠

有什么解决办法吗?
如果需要,我可以发布我的实际代码,这有点像是在动态中写的。

child1与您要在其中使用它的视图不在同一布局范围内(在“//cant do this”行)


我建议删除线性布局,并将child1和child2都放在父相对布局中。

这是因为child不是它的兄弟,Linearlayout是它的兄弟,如果您想将其移到右侧,请尝试在Linearlayout上放置一个id,并在android上使用它:layout\u toEndOf=“@+id/child1”

试试这个:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout android:id="@+id/ll_sibbling_view"
        android:background="@color/color_gray_400"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:id="@+id/child1"
            android:background="@color/colorAccent"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2">

            <TextView android:text="ABC"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="15sp"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/child2"
            android:background="@color/mab_blue"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2">
        </RelativeLayout>

    </LinearLayout>

    <View android:id="@+id/v_center_horizontal"
        android:layout_centerHorizontal="true"
        android:layout_width="0dp"
        android:layout_height="0dp"/>

    <View android:layout_toRightOf="@+id/v_center_horizontal"
        android:layout_toEndOf="@+id/v_center_horizontal"
        android:background="@color/black"
        android:layout_width="match_parent"
        android:layout_height="2dp"/>

</RelativeLayout>

好的,所以我必须通过编程来完成。我用

view.post(new Runnable() {
            @Override
            public void run() {
                child1width = child1.getWidth();
                setMarkers(); // sets margin of View (the line)
            }
        });

然后在视图中添加了一个左边距child1Width。

对,但这不会与child2重叠否?我已经编辑了答案,但这是另一种解决方案,我希望它能帮助您。是的,但这样我就无法通过权重来分离内部的子对象,我必须使用硬编码的宽度大小。