2016-12-30 21 views
0

我有這樣的代碼:如何將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="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

我想微調移動到的LinearLayout的右邊。我試圖用:

android:gravity="right" 

android:gravity="end" 

android:layout_gravity="right" 

android:layout_gravity="end" 

,但它並沒有幫助。有沒有其他選項可以將其移至右側? 什麼也很重要我想將父佈局保持爲Linear(垂直方向)和當前佈局爲Linear(水平方向)。

+1

你可以用'layout_weight = 「1」'在'TextView' –

+0

使用的FrameLayout而不是如果你設置'機器人:layout_gravity = 「權利」'來微調 – withparadox2

+0

@ChiragSavsani,你說的是veeery有趣!不過,我想知道是否有任何方法讓這個微調器儘可能小,只是把它移到右邊。 – Tomas

回答

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),你可以將組件來者優先地方

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> 

希望這個help.Happy編碼。

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

設置爲Spinner的layout_width並將layout_weight設置爲Tex tView。

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

接受這個答案,如果這對你有用。 –

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

+0

感謝@Tomas fir upvote –

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

只是channge下面的代碼中的變化。

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>