【问题标题】:CSS "float:right" property equivalent in LinearLayout on android?android上LinearLayout中的CSS“float:right”属性等效?
【发布时间】:2011-12-07 05:58:57
【问题描述】:

在 CSS 上我们可以这样写:

<div style="float:right"> Text1 </div>
<div style="float:right"> Text2 </div>

这样Text1会出现在右边..

我正在尝试对 LinearLayout 做同样的事情,视图应该从右到左显示:

<LinearLayout android:id="@+id/linearLayout1" android:layout_gravity="right" android:gravity="right"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_weight="1" android:weightSum="2" android:orientation="horizontal">
        <!-- First Column should be on the right : Text1-->
        <LinearLayout android:id="@+id/linearLayout2"
            android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:gravity="right"
            android:layout_weight="1">...</LinearLayout>
        <!-- Second Column should be on the left : Text2 -->
        <LinearLayout android:id="@+id/linearLayout3"
            android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_gravity="right" android:gravity="right"
            android:layout_weight="1">...</LinearLayout>
</LinearLayout>

谢谢

【问题讨论】:

  • 是从上到下出现的吗?
  • @LAS_VEGAS 嗯,布局一般不起作用,所以我只是想模拟 float:right 使用 LinearLayout 吗?谢谢。

标签: android css-float android-linearlayout


【解决方案1】:

可能是这个

<LinearLayout android:id="@+id/linearLayout1"
    android:gravity="right"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_gravity="right"
    >
    <!-- Second Column should be on the left : Text2 -->
    <LinearLayout android:id="@+id/linearLayout3"
        android:layout_width="wrap_content" android:layout_height="fill_parent" 
        android:layout_weight="1">...</LinearLayout>
    <!-- First Column should be on the right : Text1-->
    <LinearLayout android:id="@+id/linearLayout2"
        android:layout_width="wrap_content" android:layout_height="fill_parent"
        android:layout_weight="1">...</LinearLayout>

【讨论】:

  • 我认为您只是在物理上将 Text2 与 Text1 交换了吗?我可以这样做,但我不想?在 CSS 中我们可以让元素浮动到右边,即使它是先写的!
  • 我也把重力放在了父母身上
  • 你有 layout_gravity="right" 在父 XML 中重复了两次
【解决方案2】:

不知道 LinearLayout 是否可行,但您可以使用 RelativeLayout 实现您需要的功能,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Text1"
            android:textAppearance="@android:style/TextAppearance.Large" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/text1"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="Text1"
            android:textAppearance="@android:style/TextAppearance.Large" />
    </LinearLayout>

</RelativeLayout>

在相对布局中,您可以将“text1”布局容器相对于父级(android:layout_alignParentEnd="true"android:layout_alignParentRight="true" > 取决于 SDK 版本兼容性),然后将“Text2”容器 LinerLayout 放置在 Text1 容器的左侧(android:layout_toLeftOf="@+id/text1")。如果要添加第三个容器右对齐,只需使用相对于 Text2 容器的最后一个属性 (android:layout_toLeftOf="@+id/text2") 等等。

希望这可以帮助你。 它看起来像:

【讨论】:

  • 如何换行?我想要很多按钮的行为类似于 css-float 属性:)
  • @StefanBrendle 您可以在水平 LinerLayout 中使用此结构。 &lt;LinearLayout ··· android:orientation="horizontal"&gt; &lt;RelativeLayout&gt; ··· &lt;/RelativeLayout&gt; &lt;RelativeLayout&gt; ··· &lt;/RelativeLayout&gt; &lt;/LinearLayout&gt;
【解决方案3】:

只需添加:

android:layout_gravity="right"

gravity="right" 用于文本向右浮动,如文本对齐。

【讨论】:

    【解决方案4】:

    只需将 LinearLayout 方向设置为水平

    android:orientation="horizontal"
    

    【讨论】:

      猜你喜欢
      • 2010-12-13
      • 2015-06-11
      • 1970-01-01
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      相关资源
      最近更新 更多