2012-10-21 96 views
2

我可以將單選按鈕的位置從左至右改變。我的意思是綠色選定的按鈕將在右側,文本在左側。可能嗎? (默認按鈕向左,文本向右)更改單選按鈕的位置

+0

您可能需要爲此創建自己的單選按鈕。 – mango

+0

請參見[this](http://stackoverflow.com/questions/2631839/change-the-side-the-text-appears-on-a-radio-button)。 – Andrei

回答

0

應該可以使用getCompoundDrawables()和setCompoundDrawables()重新排列文本週圍的drawable。

爲了更進一步,也許你可以在調用super.onDraw()之後,基於CheckBox在onDraw()方法中實現自己的CheckBoxRight小部件。

最後的選擇是直接從TextView構建自己的小部件,並在onClick()事件處理程序維護內部狀態後恰當地設置compCompoundDrawables()。

0

RadioButtons不像您(或幾乎任何人)想要的那樣靈活。您可以構建自己的自定義小部件,如果您不熟悉這一部分,可能會令人生畏。或者你可以做我最喜歡的解決方法。

處理RadioButtons就好像它們是常規按鈕一樣 - 不要使用RadioGroup功能。現在必須手動控制檢查。通過消除RadioGroup,您可以隨意創建佈局。

這裏是使用單選按鈕在具有文本左和圖像的每個按鈕的右邊一個TableLayout一個示例XML佈局:

<?xml version="1.0" encoding="utf-8"?> 

<TableLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <TableRow 
     android:id="@+id/row_1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button 1" /> 

     <RadioButton 
      android:id="@+id/rb_1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="false" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/pretty_pic_1" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/row_2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button 2" /> 

     <RadioButton 
      android:id="@+id/rb_2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="false" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/pretty_pic_3" /> 
    </TableRow> 

    <TableRow 
     android:id="@+id/row_3" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:clickable="true" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button 3" /> 

     <RadioButton 
      android:id="@+id/rb_3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:clickable="false" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/pretty_pic_3" /> 
    </TableRow> 

</TableLayout> 

但是你還沒有完成。您現在必須手動處理您的單選按鈕。這裏是我喜歡做的事是:

class FooActivity extends Activity { 
RadioButton m_rb1, m_rb2; 
TableRow m_row1, m_row2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    m_rb1 = (RadioButton) findViewById(R.id.rb1); 
    m_rb2 = (RadioButton) findViewById(R.id.rb2); 

    m_row1 = (TableRow) findViewById(R.id.row_1); 
    m_row1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      m_rb1.setChecked(true); 
      m_rb2.setChecked(false);    
     } 
    }); 

    m_row2 = (TableRow) findViewById(R.id.row_2); 
    m_row2.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      m_rb1.setChecked(false);    
      m_rb2.setChecked(true); 
     } 
    }); 

} 

}

注意,我希望用戶通過選擇文本,圖片,或按鈕本身來選擇單選按鈕。所以我讓整個TableRows成爲可點擊的對象。

希望這會有所幫助!