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

时间:2016-12-30 09:56:40

标签: android layout spinner

我有这样的代码:

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

目前它看起来像是:

enter image description here

我想将Spinner移动到LinearLayout的右侧。我试着用:

android:gravity="right"

android:gravity="end"

android:layout_gravity="right"

android:layout_gravity="end"

但它没有帮助。有没有其他选择将它移到右边? 我还希望将父布局保持为线性(垂直方向),将当前布局保持为线性(水平方向)。

7 个答案:

答案 0 :(得分:1)

嘿,请检查此代码。

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

希望这有帮助。快乐编码。

答案 1 :(得分:1)

我建议将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),您可以将组件放置到首选位置

答案 2 :(得分: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"
>

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

答案 3 :(得分:1)

设置Spinner的layout_width并将layout_weight设置为TextView。

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

答案 4 :(得分: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="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

答案 5 :(得分:1)

只需更改代码中的更改即可。

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>

答案 6 :(得分:0)

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