使用match_parent,为什么文本会出现在右侧视图下方?

时间:2015-08-08 16:36:51

标签: java android android-layout android-linearlayout android-relativelayout

我不确定我理解match_parent是如何运作的 示例我很困惑:

<?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">

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

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:orientation="vertical"
            >
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/text1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="I expected this really long text to wrap around since the space on the right is already occupied but it goes bellow the red view"
                        />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/text1"
                    android:text="Bla"
                    />

            </RelativeLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_alignParentRight="true"
            >
            <View
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:background="@color/red"
            />
        </LinearLayout>

    </RelativeLayout>

</LinearLayout>

这导致以下结果:
enter image description here

因此文本扩展到父的宽度,但在红色视图下 如果我删除了视图,那么很长的文本会绕到下一行。

所以我的问题是为什么不是红色视图&#34;检测到&#34;这样文本会换行到下一行吗?

2 个答案:

答案 0 :(得分:1)

RelativeLayout有效地具有z轴,如果视图以一种方式对齐,这意味着它们的边缘与同一平面对齐,那么稍后在布局中添加的视图将位于较早的视图之上(分层),而不是将现有内容推到一边。这意味着你的红色正方形在文本视图上方/重叠,而不是在它旁边。

这不是你的问题,但也没有必要像这样嵌套你的布局,或者将各个视图包装在他们自己的布局中。编写和渲染效率非常低。您可以从开发人员教程中了解布局的差异以及何时使用它们。

我认为您需要以下内容:

<?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">

    <View android:layout_alignParentRight="true"
        android:id="@+id/shape1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@color/red"
    />

    <RelativeLayout android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/shape1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="I expected this really long text to wrap around since the space on the right is already occupied but it goes bellow the red view"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/text1"
            android:text="Bla"
            />

    </RelativeLayout>

</RelativeLayout>

解释这些变化/为什么你不需要嵌套:

  • 你不需要最外面的LinearLayout因为它只有一个孩子
  • 出于同样的原因你不需要红色方块周围的LinearLayout,只需移动android:layout_alignParentRight =&#34; true&#34;到红场视图
  • 出于同样的原因,你不需要包含hte TextViews的RelativeLayout周围的LinearLayout。移动android:layout_alignParentLeft =&#34; true&#34;到RelativeLayout孩子
  • 然后你可以通过确保它是对孩子的RelativeLayout或孩子的RelativeLayout结束toLeftOf红色正方形来阻止文本与文本重叠,就像我的例子一样。
  • 如果你想进一步改进,可以删除子RelativeLayout并将toLeftOf和android:layout_alignParentLeft =&#34; true&#34;在第一个TextView和android:layout_alignLeft =&#34; @ + id / text1&#34;加上android:layout_alignRight =&#34; @ + id / text1&#34;在第二个TextView上,你仍然会得到相同的结果

答案 1 :(得分:1)

问题不在于match_parent的{​​{1}},而是线性布局的TextView

fill_parent

这将使其与其父级一样宽,在这种情况下是整个屏幕。您可以尝试添加<LinearLayout android:layout_width="fill_parent"

另一种选择是使用水平 LinearLayout来保持文本视图不在红色框下面。你可以使用

android:layout_toLeftOf="@id/theOtherLinearLayout"

表示视图应在此布局中使用所有剩余空间(在固定元素之后,在这种情况下,红色框已定位)。