2017-12-07 5 views
1

라디오 버튼의 텍스트를 프로그래밍 방식으로 위에서 아래로 정렬 할 수있는 방법이 있습니까? 라디오 버튼 텍스트를 프로그래밍 방식으로

enter image description here

제가

라디오 그룹
final RadioButton[] rb = new RadioButton[5]; 
      RadioGroup rg = new RadioGroup(getActivity()); //create the RadioGroup 
      rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL 
      rg.setLayoutParams(radioparams); 

      for (int i = 0; i < 5; i++) { 
       rb[i] = new RadioButton(getActivity()); 
       rb[i].setText("Radiobtn " + i); 
       rb[i].setId(i + 100); 
       rg.addView(rb[i]); 

      } 
      layout.addView(rg); 

을 만들기위한 다음 코드를 사용하지만, 각 버튼의 우측으로 텍스트를 얻는다.

+0

https://stackoverflow.com 같은 프로그래밍 방식 RadioButton를 추가보다이

<LinearLayout android:id="@+id/lnrView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="80dp" android:orientation="horizontal"> </LinearLayout> 

같은 레이아웃을 당신이 트릭

을 할 수있는이 방법을 시도/questions/19102159/radiobutton-with-text-above-button –

+0

프로그래밍 방식으로 구현하고 싶습니다. –

+0

사용자 정의 라디오 버튼을 작성하여 뷰에 동적으로 추가하여이를 수행 할 수 있습니다. 참조 : https://crosp.net/blog/android/creating-custom-radio-groups-radio-buttons-android/ – Saneesh

답변

3
final RadioButton[] rb = new RadioButton[5]; 
    RadioGroup rg = new RadioGroup(getActivity()); //create the RadioGroup 
    rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL 
    rg.setLayoutParams(radioparams); 

    for (int i = 0; i < 5; i++) { 
     rb[i] = new RadioButton(getActivity()); 
     rb[i].setText("Radiobtn " + i); 
     rb[i].setId(i + 100); 
     rb[i].setButtonDrawable(null); 

     TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {android.R.attr.listChoiceIndicatorSingle}); 
     int attributeResourceId = a.getResourceId(0, 0); 
     Drawable drawable = getResources().getDrawable(attributeResourceId); 

     rb[i].setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable); 
     rb[i].setGravity(Gravity.CENTER | Gravity.BOTTOM); 
     rg.addView(rb[i]); 

    } 
    layout.addView(rg); 
+0

감사합니다. 나를 위해 일했다.) –

+0

어떻게 라디오 버튼 색상을 변경할 수 있습니다. ? –

+1

'DrawableCompat.setTint (drawable, Color.BLUE); ' – mac229

1

button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 


      LinearLayout layout = new LinearLayout(MainActivity.this); 

      layout.setOrientation(LinearLayout.HORIZONTAL); 
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 


      LinearLayout layout2 = new LinearLayout(MainActivity.this); 
      layout2.setOrientation(LinearLayout.VERTICAL); 
      LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT); 


      final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
      final TextView textView = new TextView(MainActivity.this); 
      int id = 0; 
      textView.setId(id); 
      textView.setTextSize(14); 
      textView.setTextColor(Color.rgb(0, 0, 0)); 
      textView.setMaxEms(2); 
      textView.setText("NIlu"); 
      layout2.addView(textView); 

      RadioButton radioButton = new RadioButton(MainActivity.this); 
      layout2.addView(radioButton); 
      layout.setLayoutParams(lp); 

      lnrView.addView(layout2); 


     } 
    }); 
+0

라디오 버튼 텍스트를 맨 위에 놓기를 원하므로 textview를 사용해야합니다. ? –

관련 문제