如何将 LinearLayout 中的微调器向右移动?

How to move spinner in LinearLayout to right?

我有这样的代码:

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

<LinearLayout
    android:id="@+id/relativeLayoutforPRICE"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView$_points_to_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Points to win"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner$_points_to_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>

目前看起来是这样的:

我想将 Spinner 移到 LinearLayout 的右侧。我尝试使用:

android:gravity="right"

android:gravity="end"

android:layout_gravity="right"

android:layout_gravity="end"

但这并没有帮助。有没有其他选项可以将它移到右边? 同样重要的是,我希望将父布局保持为线性(垂直方向),将当前布局保持为线性(水平方向)。

嘿,检查这个代码。

  <LinearLayout
    android:id="@+id/relativeLayoutforPRICE"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="2">

    <TextView
        android:id="@+id/textView$_points_to_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Points to win"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner$_points_to_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

希望这个help.Happy编码。

我建议使用RelativeLayout这个逻辑:

<RelativeLayout
    android:id="@+id/relativeLayoutforPRICE"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/textView$_points_to_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Points to win"
        android:layout_alignParentLeft="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner$_points_to_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"/>

</RelativeLayout>

通过父对齐属性(即 layout_alignParentLeft、layout_alignParentRight),您可以将组件放置到首选位置

请使用相对布局而不是线性布局

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

<RelativeLayout
    android:id="@+id/relativeLayoutforPRICE"
    android:layout_width="match_parent"
    android:layout_gravity="right"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView$_points_to_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Points to win"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner$_points_to_win"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</RelativeLayout>
</LinearLayout>

将 Spinner 设为 layout_width,将 TextView 设为 layout_weight

<LinearLayout
    android:id="@+id/relativeLayoutforPRICE"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView$_points_to_win"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Points to win"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner$_points_to_win"
        android:layout_width="50dp"
        android:layout_height="wrap_content"/>

</LinearLayout>

试一下这是你的代码我已经更新了一点。

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

    <LinearLayout
        android:id="@+id/relativeLayoutforPRICE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textView$_points_to_win"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Points to win"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Spinner
            android:id="@+id/spinner$_points_to_win"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

这是截图,效果很好

http://screencast.com/t/kD5y6PlVi

将您的 LinearLayout 更改为以下内容:

<LinearLayout
        android:id="@+id/relativeLayoutforPRICE"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

只需更改您的代码即可。

TextView

layout_width="0dp" 
layout_weight="1"

参见代码。

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

<LinearLayout
    android:id="@+id/relativeLayoutforPRICE"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textView$_points_to_win"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        layout_weight="1"
        android:text="Points to win"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Spinner
        android:id="@+id/spinner$_points_to_win"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>