Android RadioButtons仅在模拟器上行为不端

时间:2013-01-21 10:20:27

标签: android android-layout android-emulator

我在xml中有RadioGroup。我从代码中填充了RadioButton。 问题是,在Emulators中RadioButtons不表现w.r.t RadioGroup。我可以一次选择所有的radiobuttons。但是在真实设备上似乎一切正常。

以下是所有文件的屏幕截图和代码:

模拟器的屏幕截图

Screenshot from emulator

来自设备Samsung Galaxy的屏幕截图

Screenshot from device Samsung galaxy

MainActivity.java的来源

public class MainActivity extends Activity {
/** Called when the activity is first created. */
private RadioGroup rg;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    LayoutInflater inflater = LayoutInflater.from(getBaseContext());
    rg = (RadioGroup)findViewById(R.id.rg);

    for (int i = 0; i < 5; i++) {
        inflater.inflate(R.layout.myradio, rg,true);
    }

}

}

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/llContainer"
    tools:context=".MainActivity" >


<RadioGroup 
    android:id="@+id/rg"
    android:background="#0000ff"
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">

</RadioGroup>

myradio.xml

<?xml version="1.0" encoding="utf-8"?>

<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="RadioButton" />

1 个答案:

答案 0 :(得分:0)

就像这样

public class example extends Activity {
    /** Called when the activity is first created. */
    private RadioGroup rgroup;
     RadioButton rb;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.example);


        rgroup = (RadioGroup)findViewById(R.id.rg);
        final RadioButton[] rb = new RadioButton[5];
        for (int i = 0; i < 5; i++) {
            rb[i]=new RadioButton(this);
            rb[i].setText("rdo"+i);
            rb[i].setId(i);
            rgroup.addView(rb[i]);


        }

    }

    }

enter image description here