动态创建分段单选按钮

时间:2013-12-12 12:57:40

标签: android

我创建了Segmented RadioButtons,它的工作正常。我创建了它Statically,现在我想要创建相同的按钮Dynamically,我尝试了它,它不起作用。
创建的Static Segmented RadioButtons如下所示:enter image description here

我想要如下内容:
enter image description here

我正在尝试如下:

<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上课为:

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);
        }
    }

}

GameBoardActivity班级

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;
    }

}

我的输出如下:
enter image description here
I want output as I mentioned above.. Where and what I'm doing mistakes...

1 个答案:

答案 0 :(得分: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