2013-12-12 5 views
1

Segmented RadioButtons을 작성했으며 올바르게 작동합니다. 나는 Statically을 만들었고, 지금은 같은 버튼을 만들고 싶습니다. Dynamically을 시도했는데 작동하지 않았습니다.세그먼트 라디오 버튼을 동적으로 생성하십시오.

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#BCC6CC" > 
    <com.sushi19.myProj.segmented.SegmentedRadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dip" 
     android:layout_gravity="center_horizontal" 
     android:orientation="horizontal" 
     android:id="@+id/segment1" 
     android:checkedButton="@+id/btn1" > 

     <RadioButton 
      android:id="@+id/btn1" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:button="@null" 
      android:gravity="center" 
      android:minHeight="33dip" 
      android:minWidth="40dip" 
      android:text="@string/segmentedRadio1" 
      android:textAppearance="@dimen/text_size" 
      android:textColor="@drawable/radio_text_color" /> 

     <RadioButton 
      android:id="@+id/btn2" 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:button="@null" 
      android:gravity="center" 
      android:minHeight="33dip" 
      android:minWidth="40dip" 
      android:text="@string/segmentedRadio2" 
      android:textAppearance="@dimen/text_size" 
      android:textColor="@drawable/radio_text_color" /> 

    </com.sushi19.myProj.segmented.SegmentedRadioGroup> 
</LinearLayout> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#D1D0CE" > 
    <com.sushi19.myProj.segmented.SegmentedRadioGroup 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dip" 
     android:layout_gravity="center_horizontal" 
     android:orientation="horizontal" 
     android:id="@+id/segment2" 
     android:checkedButton="@+id/btn3" > 

     <RadioButton 
      android:id="@+id/btn4" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:button="@null" 
      android:gravity="center" 
      android:minHeight="33dip" 
      android:minWidth="40dip" 
      android:text="@string/segmentedRadio4" 
      android:textAppearance="@dimen/text_size" 
      android:textColor="@drawable/radio_text_color" /> 

     <RadioButton 
      android:id="@+id/btn3" 
      android:layout_width="150dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:button="@null" 
      android:gravity="center" 
      android:minHeight="33dip" 
      android:minWidth="40dip" 
      android:text="@string/segmentedRadio3" 
      android:textAppearance="@dimen/text_size" 
      android:textColor="@drawable/radio_text_color" /> 

    </com.sushi19.myProj.segmented.SegmentedRadioGroup> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/layoutFilterContent" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:baselineAligned="false" 
    android:background="#E5E4E2" > 

    <ScrollView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.25" > 

     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#BCC6CC" 
      android:orientation="vertical" > 
      <com.sushi19.myProj.segmented.SegmentedRadioGroup 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="5dip" 
       android:layout_gravity="center_horizontal" 
       android:orientation="vertical" 
       android:id="@+id/segment_buttons" > 
      </com.sushi19.myProj.segmented.SegmentedRadioGroup> 
     </LinearLayout> 
    </ScrollView> 

    <HorizontalScrollView  
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="0.75" > 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#E5E4E2" 
      android:orientation="horizontal" > 
     </LinearLayout> 
    </HorizontalScrollView> 

</LinearLayout>  

SegmentedRadioGroup 클래스로 다음과 같이 내가 노력하고있어
enter image description here

을 다음과 같이 같은 enter image description here

내가 뭔가를 원하는 :
은 다음과 같은 StaticSegmented RadioButtons 보이는 생성 :

package com.sushi19.myProj.segmented; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.widget.RadioGroup; 

public class SegmentedRadioGroup extends RadioGroup { 

    public SegmentedRadioGroup(Context context) { 
     super(context); 
    } 

    public SegmentedRadioGroup(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onFinishInflate() { 
     super.onFinishInflate(); 
     changeButtonsImages(); 
    } 

    private void changeButtonsImages(){ 
     int count = super.getChildCount(); 

     if(count > 1){ 
      super.getChildAt(0).setBackgroundResource(com.sushi19.myProj.R.drawable.segment_radio_left); 
      for(int i=1; i < count-1; i++){ 
       super.getChildAt(i).setBackgroundResource(com.sushi19.myProj.R.drawable.segment_radio_middle); 
      } 
      super.getChildAt(count-1).setBackgroundResource(com.sushi19.myProj.R.drawable.segment_radio_right); 
     }else if (count == 1){ 
      super.getChildAt(0).setBackgroundResource(com.sushi19.myProj.R.drawable.segment_button); 
     } 
    } 

} 

다음과 같이 I 출력을 얻고있다

package com.sushi19.myProj; 


import com.sushi19.myProj.segmented.SegmentedRadioGroup; 

import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.app.ActionBar.LayoutParams; 
import android.app.Activity; 
import android.graphics.Color; 
import android.util.TypedValue; 
import android.view.Gravity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.RadioButton; 

public class GameBoardActivity extends Activity { 

    RadioButton rb; 
    SegmentedRadioGroup srg; 


    @SuppressLint({ "NewApi", "InlinedApi", "ResourceAsColor" }) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.game_board); 

     srg = (SegmentedRadioGroup)findViewById(R.id.segment_buttons); 

     LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

     for(int i=0; i<15; i++){ 
      rb = new RadioButton(this); 
      rb.setId(i); 
      rb.setGravity(Gravity.CENTER); 
      //convert pixels to dip 
      int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 33, getResources().getDisplayMetrics()); 
      int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics()); 
      rb.setMinHeight(height); 
      rb.setMinWidth(width); 
      rb.setText("Button"+i); 
      rb.setButtonDrawable(null); 
      rb.setTextSize(getResources().getDimension(R.dimen.text_size)); 
      rb.setTextColor(R.drawable.radio_text_color); 
      rb.setLayoutParams(params); 
      srg.addView(rb); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.game_board, menu); 
     return true; 
    } 

} 

클래스 GameBoardActivity에서 :
enter image description here
I want output as I mentioned above.. Where and what I'm doing mistakes...

답변

0

나는 동일한 구조를 사용했습니다.

이 도움이 될 수 있습니다

* setBackgroundResource

 int total=15; 
     for(int i=0; i<total; i++){ 
     rb = new RadioButton(getActivity()); 
     rb.setId(i); 
     rb.setGravity(Gravity.CENTER); 
     //convert pixels to dip 
     int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 33, getResources().getDisplayMetrics()); 
     int width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, getResources().getDisplayMetrics()); 
     rb.setMinHeight(height); 
     rb.setMinWidth(width); 

     //addition 
     if(i==0){rb .setBackgroundResource(com.xxxxx.yyyy.R.drawable.segment_radio_left);} 
     else if(i==total-1){rb .setBackgroundResource(com.xxxxx.yyyyy.R.drawable.segment_radio_right);} 
     else{rb .setBackgroundResource(com.xxxxxx.yyyyy.R.drawable.segment_radio_middle);} 

     rb.setText("Button"+i); 
     rb.setButtonDrawable(null); 
     //rb.setTextSize(getResources().getDimension(R.dimen.text_size)); 
     //rb.setTextColor(R.drawable.radio_text_color); 
     rb.setLayoutParams(params); 
     srg.addView(rb); 
    } 

enter image description here

관련 문제