簡體   English   中英

如何在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>

希望對您有所幫助。

我建議對這個邏輯使用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>

設置微調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>

試試這個它的代碼我已經更新了一點。

  <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

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

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>

將您的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">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM