与相对布局的右侧对齐似乎不起作用

时间:2015-11-30 19:07:48

标签: android android-relativelayout

我正在尝试将两个文本视图放在父容器右侧的另一个旁边 类似的东西:

from zipfile import ZipFile
from urllib import urlretrieve
from tempfile import mktemp

filename = mktemp('.zip')
destDir = mktemp()
theurl = 'http://www.example.com/file.zip'
name, hdrs = urlretrieve(theurl, filename)
thefile=ZipFile(filename)
thefile.extractall(destDir)
thefile.close()

以下不起作用,但我无法理解为什么

+++++++++++++++++++++++++++++++++++++  (parent container)  
|               TextView2 TextView1 |  
+++++++++++++++++++++++++++++++++++++    

注意:100dp只是为了显示问题

更新
删除android后:layout_alignParentTop =“true”和android:layout_alignParentRight =“true”我得到:

enter image description here

2 个答案:

答案 0 :(得分:4)

在第二个TextView上,使用android:layout_toLeftOf="@id/textView1"并移除android:layout_alignParentTop="true"android:layout_alignParentRight="true"

像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="100dp"
              android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="TEXTVIEW-1"
            />
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/textView1"
            android:layout_marginRight="1dp"
            android:text="TEXTVIEW-2"
            />

    </RelativeLayout>  

</LinearLayout>

实际做的是:

当你在两个textViews中使用android:alignParentRight="true"时,它会将textViews放在右侧并相互重叠。

因此,仅将其添加到一个textView时,它将仅将其与右侧对齐。然后添加toLeftOf将它放在第一个textView的左侧。

  

更新:

我没有找到任何关于如何给予RelativeLayout优先权的问题的文档。但是,我深入了解RelativeLayout.java文件以找出答案。这有点难以理解。

所以我尝试使用选项和我发现的内容:

  • 如果您申请android:layout_alignParentStart="true"android:layout_alignParentEnd="true",优先权将提供给 android:layout_alignParentStart

  • 如果您申请android:layout_toStartOf="@id/view2"android:layout_toEndOf="@id/view2",优先权将被提供给 android:layout_toEndOf

  • 如果您申请android:layout_alignParentStart="true"android:layout_toStartOf="@id/view1",优先权将提供给 android:layout_alignParentStart

  • 如果你申请android:layout_alignParentStart="true"android:layout_centerHorizontal="true"android:layout_toStartOf="@id/view1"

    然后按优先顺序给出

     android:layout_alignParentStart="true"   //1 High
     android:layout_toStartOf="@id/view1"     //2 Medium
     android:layout_centerHorizontal="true"   //3 Low
    

    因此,这意味着android:layout_centerHorizontal仅在android:layout_alignParentStartandroid:layout_toStartOf不存在时才有效。

希望现在很清楚。 如果有人找到了相关的文档,我很乐意阅读优先事项。

答案 1 :(得分:0)

  android:layout_alignParentRight="true"

从textView2

中删除它