2016-04-28 78 views
0

如何讓我的複選框在右側對齊並在文本後面有複選框符號?我瞄準API 15歲及以上複選框對齊權

<LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout1"> 
     <TextView 
      android:text="@string/please_choose_an_area" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:id="@+id/textView2" 
      android:textSize="20sp" 
      android:gravity="center" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="15dp" /> 
     <CheckBox 
      android:text="All" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/checkBox1" 
      android:layout_marginRight="15dp" /> 
    </LinearLayout> 
+1

檢查,如果你可以用'CheckedTextView'。 – jaibatrik

+0

檢查此問題http://stackoverflow.com/questions/3156781/how-to-show-android-checkbox-at-right-side – Amy

回答

2
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:gravity="center" 
     android:weightSum="6" 
     > 
     <TextView 
      android:text="place to select" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:id="@+id/textView2" 
      android:textSize="20sp" 
      android:layout_weight="5.8" 
      android:layout_marginLeft="10dp" /> 
     <CheckBox 
      android:layout_weight="0.2" 
      android:text="All" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/checkBox1" 
      android:gravity="right" 
      android:layout_marginRight="10dp" 
      /> 
     </LinearLayout> 
    </LinearLayout> 
+0

我認爲沒有必要採用嵌套的'線性佈局',因爲它只用單個'線性佈局'來完成,就像我的答案一樣。 –

+0

@nostradamus,它將如何被接受的答案? –

2

添加這2行代碼與你的CheckBox

android:button="@null" 
android:drawableRight="?android:attr/listChoiceIndicatorMultiple" 

喜歡這種方式。

<CheckBox 
    android:text="All" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/checkBox1" 
    android:layout_marginRight="15dp" 
    android:button="@null" 
    android:drawableRight="?android:attr/listChoiceIndicatorMultiple"/> 
+0

不錯,但現在我怎麼能對齊到正確的? – Nostradamus

+0

添加這個機器人':重力=「右| center_vertical」' –

+1

肯定的,而不是添加TextView的分開,這是很好的方法! –

1

使用的RelativeLayout內的LinearLayout像下面

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
     android:gravity ="end" 
> 

<TextView 
    android:id="@+id/text" 
    android:text="myText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@+id/chekcbox" 
/> 

<CheckBox 
    android:id="@+id/chekcbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 

/> 

</RelativeLayout> 
1

試試這個代碼。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="15dp" 
     android:gravity="center" 
     android:text="area" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textSize="20sp" /> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="right|center_vertical" 
     android:layout_marginRight="15dp" 
     android:button="@null" 
     android:drawableRight="?android:attr/listChoiceIndicatorMultiple" 
     android:text="All" /> 
</LinearLayout> 
+0

'機器人:layout_gravity = 「右| center_vertical」'應該是'安卓重力= 「右| center_vertical」',因爲'layout_width'是'match_parent' –

+1

@ZahidulIslam誤認爲我已經編輯感謝信息。 –

+0

用Upvote欣賞,如果它有用:)。 –