setTextColor()和setTextSize()不适用于Android中动态创建的单选按钮

时间:2013-12-13 13:36:37

标签: android

我也创建了RadioGroup按钮DynamicallyStatic。一切都适用于静态RadioButtons,但setTextColor()setTextSize()不适用于动态创建的RadioButtons。我在下面的代码中包含快照...

layout_activity.xml     

        <!-- Here Dynamic -->
        <ScrollView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="0.25" >
            <LinearLayout
                android:id="@+id/layoutTopicGrade"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#BCC6CC"
                android:orientation="vertical" >
                <RadioGroup 
                    android:id="@+id/radio_group"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="5dip"
                    android:paddingRight="5dip"
                    android:paddingBottom="5dip"
                    android:orientation="vertical" >
                </RadioGroup>
            </LinearLayout>
        </ScrollView>

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

                <RadioGroup 
                    android:id="@+id/radiogroup"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dip"
                    android:orientation="vertical" >

                <RadioButton
                    android:id="@+id/radio1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:gravity="center"
                    android:minHeight="33dip"
                    android:minWidth="40dip"
                    android:text="RadioButton1"
                    android:button="@null"
                    android:textAppearance="@dimen/text_size"
                    android:textColor="@drawable/radio_text_color"
                    android:background="@drawable/buttonbackground"
                    android:layout_marginTop="5dip"
                    android:checked="true" />   

                <RadioButton
                    android:id="@+id/radio2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/buttonbackground"
                    android:button="@null"
                    android:minHeight="33dip"
                    android:minWidth="40dip"
                    android:layout_marginTop="5dip"
                    android:gravity="center"
                    android:textAppearance="@dimen/text_size"
                    android:textColor="@drawable/radio_text_color"
                    android:text="RadioButton2" />    

                <RadioButton
                    android:id="@+id/radio3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:background="@drawable/buttonbackground"
                    android:button="@null"
                    android:minHeight="33dip"
                    android:minWidth="40dip"
                    android:layout_marginTop="5dip"
                    android:gravity="center"
                    android:textAppearance="@dimen/text_size"
                    android:textColor="@drawable/radio_text_color"
                    android:text="RadioButton3" />
                </RadioGroup>
            </LinearLayout>
        </HorizontalScrollView>

    </LinearLayout>

我已创建values-ldpivalues-mdpivalues-hdpi个文件夹,依此类推。在这些文件夹中,我创建了dimen.xml文字大小为14dip16dip18dip的内容,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="text_size">20dip</dimen>
</resources>

radio_text_color.xml as:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:state_checked="true" 
        android:color="#FFF" />

    <item  android:color="#000" />
</selector>

buttonbackground.xml as:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:drawable="@drawable/segment_white"
        android:state_checked="true"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/segment_grey_press"
        android:state_pressed="true" />
    <item
        android:drawable="@drawable/segment_grey_press"
        android:state_checked="true" />
    <item
        android:drawable="@drawable/segment_white" />
</selector>

mainActivity.java as:

public class mainActivity extends Activity {

    RadioButton rb;
    RadioGroup rg;


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

        rg = (RadioGroup)findViewById(R.id.radio_group);

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

        for(int i=0; i<15; i++){
            rb = new RadioButton(this);
            rb.setId(i);
            rb.setTag("Tag"+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.setChecked(true);
            rb.setButtonDrawable(new StateListDrawable());
            rb.setTextSize(getResources().getDimension(R.dimen.text_size));
            rb.setTextColor(getResources().getColor(R.drawable.radio_text_color));
            rb.setBackgroundResource(R.drawable.yourbuttonbackground);
            params.topMargin = 5;
            rb.setLayoutParams(params);
            rg.addView(rb);
            rb.setOnClickListener(buttonClicked);
        }
    }

    public OnClickListener buttonClicked = new OnClickListener() {
        public void onClick(View v) {
            int _id =  v.getId();
            String str = v.getTag().toString();
            Toast.makeText(getBaseContext(),"str: "+str,Toast.LENGTH_SHORT).show();
        }
    };

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

}

Output Screenshot
enter image description here
正如您在屏幕截图的右侧(静态按钮)中看到的那样,选择了第二个按钮,所选按钮的文本颜色为White,其中剩下的两个是Black,在左侧(动态按钮)文本颜色不会改变,默认情况下它是黑色。即使按钮的高度也不同,但我在两个地方都使用相同的属性 How to get same output either it is Dynamic or Static?

1 个答案:

答案 0 :(得分:0)

尝试为ex:

设置颜色状态列表
ColorStateList myColorStateList = new ColorStateList(
            new int[][]{
                    new int[]{android.R.attr.state_pressed}, //1
                    new int[]{android.R.attr.state_focused}, //2
                    new int[]{android.R.attr.state_focused, android.R.attr.state_pressed} //3
            },
            new int[] {
                Color.RED, //1
                Color.GREEN, //2
                Color.BLUE //3
            }
        );
    rb.setTextColor(myColorStateList);

或只是致电

rb.setTextColor(getResources().getColorStateList(R.drawable.radio_text_color));