2010-06-25 216 views

回答

9

我不認爲你可以。看看source code,似乎勾選標記始終顯示在右側。而且,說實話,我會建議你堅持這一點,以保持一致性 - 由於Android的應用程序具有不一致的用戶界面,所以Android一直處於崩潰狀態。

在起飛的機會,有人在你的頭用槍指着,迫使你改變對號的位置,子類CheckedTextView作爲OMGPleaseDontShootCheckedTextView,並覆蓋像onDraw()方法,與原來的扭捏版本改變的位置圖像和文字。

+0

感謝,這是很有幫助! – 2010-06-25 04:51:41

+7

現在材質設計的檢查標記應該在左邊,但是允許這個的代碼不幸是私人的,所以不能很容易地向後移動。 – 2015-08-04 14:29:08

28

創建您自己的行模板並在CheckedTextView上設置android:drawableLeft。

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1" 
android:layout_width="fill_parent" 
android:layout_height="40dip" 
android:gravity="center_vertical" 
android:paddingLeft="5dip" 
android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" 
/> 

android:drawableLeft="?android:attr/listChoiceIndicatorSingle" 
+2

這會阻止您將複合可繪製功能與複選標記指示符(缺省情況下,CheckedTextView允許)一起使用。我認爲@CommonsWare的答案是比較合適的。 – 2013-04-30 20:44:53

-4

機器人:drawableLeft = 「機器人:ATTR/listChoiceIndicatorMultiple」:P

25

的祕密是在android系統:複選標記,使其空

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/checked_text_single_choice" 
android:layout_width="match_parent" 
android:layout_height="52dp" 
android:checkMark="@null" 
android:gravity="center_vertical" 
android:textAppearance="?android:attr/textAppearanceSmall" 
android:drawableLeft="?android:attr/listChoiceIndicatorSingle" 
android:drawableRight="@null" 
android:textColor="@android:color/black" 
/> 
+2

我不相信這個工作。謝謝 – j2emanue 2014-05-13 18:29:26

+0

工作解決方案 – vuhung3990 2016-12-12 02:44:44

+1

這是什麼魔術?它甚至不合邏輯,但工作! – Ogbe 2017-09-08 07:20:47

0

嘗試這一個,並希望它會給你一些想法

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" > 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Connection lost sound alert" 
       android:layout_weight="10" 
       android:layout_gravity="center" 
      /> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="2"  
      android:layout_gravity="right" 
      android:orientation="horizontal" > 

      <CheckBox android:id="@+id/checkbox_meat" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right"     
       android:onClick="onCheckboxClicked"/> 

     </LinearLayout> 


    </LinearLayout> 
7

如果您希望複選框位於左側,只需使用CheckBox即可。也許這當然不是問題,但CheckBox可以包含文本。您可以通過添加XML屬性android:text或調用setText()方法來定義該文本。其實CheckBoxButton繼承,它從TextView繼承,這就是爲什麼它具有所有文本相關的屬性。

+0

在單獨的答案中添加了帶有屏幕截圖的「CheckBox」xml示例 – 2015-02-26 23:13:54

2

@ WonderCsabo建議使用CheckBox的示例。

<CheckBox 
    android:id="@+id/create_account_checkedTextView_password" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Show" 
    android:gravity="center" 
    android:checked="true" 

    # relative layout params 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:layout_above="@+id/hint1" /> 

checkbox_example

2

有可與可選中佈局一招想在這個崗位 - https://chris.banes.me/2013/03/22/checkable-views/ 。只需使用一些自定義的佈局像這樣的ListView項佈局:

<com.example.custom_view.CheckableLinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 
<CheckedTextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checkMark="?android:attr/listChoiceIndicatorSingle" 
    android:duplicateParentState="true" 
    .../> 
<TextView 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    .../> 
</com.example.custom_view.CheckableLinearLayout> 

android:checkMark=?android:attr/listChoiceIndicatorMultiple" 
2

您可以將您繪製到drawableLeft屬性,但它不會讓你有任何好處,因爲它不支持着色。 相反,我伸出CheckedTextView這樣的:

public class LeftSideCheckedTextView extends CheckedTextView { 

    public LeftSideCheckedTextView(Context context) { 
     this(context, null, 0); 
    } 

    public LeftSideCheckedTextView(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public LeftSideCheckedTextView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     Field mCheckMarkGravity = null; 
     try { 
      mCheckMarkGravity = this.getClass().getSuperclass().getDeclaredField("mCheckMarkGravity"); 
      mCheckMarkGravity.setAccessible(true); 
      mCheckMarkGravity.set(this, Gravity.START); 
     } catch (Exception e) { 
      Logger.error(e); 
     } 
    } 
} 

在這裏,我訪問其內部使用CheckedTextView但必須通過樣式屬性無訪問權限的隱藏字段mCheckMarkGravity,根據這個bug票:https://code.google.com/p/android/issues/detail?id=174517