【问题标题】:LinearLayout: how to place a view below/right of a fill_parent view?LinearLayout:如何在 fill_parent 视图的下方/右侧放置视图?
【发布时间】:2012-06-14 01:01:34
【问题描述】:

我想在左边有一个填充空间的 TextView,在右边有一个右对齐的 TextView。我试过摆弄重力、layout_gravity,并让第二个 TextView fill_parent 也一样,但只要第一个是 fill_parent,第二个 TextView 总是不可见。我注意到如果 LinearLayout 是垂直的,也会出现同样的问题。怎么办?

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="fill_horizontal"
    android:orientation="horizontal" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Left Side"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Right side"
        android:background="#ff606060"/>
</LinearLayout>

目标:Android 2.2

【问题讨论】:

  • 这是因为 fill_parent 正在将您最右侧的 textview 推离屏幕。权重是做到这一点的方法。我不得不自己做这件事。在我看来,谷歌会实现一个 fills_void 功能,这样如果你有一个视图想要保持静态,而另一个视图想要填充其余部分,它就会这样做。

标签: android android-linearlayout


【解决方案1】:

我想这就是你想要的。 如果您不想换行,也可以将 singleLine(和 ellipsize)添加到左视图。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Left Side"
        android:layout_weight="1"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Right side"
        />
</LinearLayout>

【讨论】:

  • android:singleLine="true" 似乎足够了;文本默认为椭圆形。
  • 你说得对,我最近好像添加了一些多余的属性。
【解决方案2】:

在您的TextView 上使用layout_weight 并将layout_width 设置为0dp。 所以在第一个 TextView 上将权重设置为 3,然后在第二个上设置为 1/

【讨论】:

  • 谢谢。实际上layout_width="0" 有一个不希望的效果(TextViews 的大小不根据它们的内容)所以我只是在两个视图上设置layout_width="wrap_content",然后在左侧视图上设置layout_weight="1" (或任何正数)以使其填充空间. gravity 属性似乎没有任何作用,因此我将其删除。
【解决方案3】:

为什么不使用RelativeLayout?

将右对齐TextView设置为layout_alignParentRight,然后将另一个填充空间的设置为layout_alignParentLeft和layout_toLeftOf右对齐TextView

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2016-06-08
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    相关资源
    最近更新 更多