2014-04-25 73 views
2

我需要在無線組中動態創建RadioButton,無線組已在XML中定義,在進行了我的研究後,我發現一些主題將android:drawableRight="@android:drawable/btn_radio"android:button="@null"置於XML如何以編程方式執行它們?Android RadioButton阿拉伯語(文本在左邊)以編程方式

這裏是我的代碼:

final RadioGroup RG =(RadioGroup) vi.findViewById(R.id.RG); 
RG.removeAllViews(); 
String[] answers= null; 
answers=item.Answers.split(";"); 
final TextView txtTitle = (TextView) vi.findViewById(R.id.QuestionRow); 
txtTitle.setText(item.Question); 
for (int i=0;i<answers.length;i++){    

     RadioButton bt=null; 
     bt = new RadioButton(parent.getContext()); 
     bt.setId(i+1); 
     bt.setText(answers[i]);  
     bt.setGravity(Gravity.RIGHT);    
     RG.addView(bt); 
} 

這裏是我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/Questionlayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_gravity="right" 
android:background="@color/White" > 

<TextView 
    android:id="@+id/QuestionRow" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:gravity="right" 
    android:textColor="@color/black_overlay" 
    android:textSize="30sp" 
    android:textStyle="bold" /> 

<RadioGroup 
    android:id="@+id/RG" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="right" 
    android:layout_below="@+id/QuestionRow" 
    android:layout_alignRight="@+id/QuestionRow"   
    ></RadioGroup> 

<Button 
    android:id="@+id/Vote" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/RG" 
    android:text="Vote" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_below="@+id/RG" 
    android:text="See Statistics" /> 

</RelativeLayout> 
+0

歡迎的StackOverflow!你試過了什麼? – Llopis

+0

您是否閱讀過文檔? 'setButtonDrawable'? – njzk2

+0

該代碼是在一個適配器,填充ListView和Radiobutton默認情況下顯示其正確的文本我需要的是顯示左側的文本,因爲阿拉伯文寫作從右到左開始我需要將drawableRight abd android:button到代碼... – Albert

回答

0
protected void onPostExecute(HashMap<String,String> mylist) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(mylist); 
     for(Integer z=0;z<mRG.getChildCount();z++){ 
      View o =mRG.getChildAt(z); 
      ((RadioButton)o).setText(text[z]); 







    } 

     for(Integer z =0;z< mRG.getChildCount();z++){ 

      Set set = mylist.entrySet(); 
       Iterator i = set.iterator(); 
      View o = mRG.getChildAt(z); 

       NumberFormat formatter = new DecimalFormat("#0.00"); 

      if (o instanceof RadioButton){ 


      int counted[] = new int[ mRG.getChildCount()]; 
      int j=0; 
      while(i.hasNext()) { 


    enter code here 
        Map.Entry me = (Map.Entry)i.next(); 
        String Value=String.valueOf(formatter.format(Double.valueOf(me.getValue().toString())*100/count))+"%"; 
        if (Integer.valueOf(me.getKey().toString())==((RadioButton) o).getId()) 
        { 

         ((RadioButton)o).setText(text[z]+" "+ Value); 
        } 



      } 
     } 
     } 
     progressDialog.hide(); 
    }