1. 程式人生 > >QQ介面圖片在上文字在下

QQ介面圖片在上文字在下

主xml的佈置

<RadioButton
            android:padding="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@null"
            android:text="111"
            android:background="@drawable/selector_01"
            android:gravity="center"
            android:layout_weight="1"/>

selector_01的配置

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/shape_01" android:state_checked="true"/>
</selector>

shape_01的配置

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#0ff"/>

</shape>

主java介面

public class MainActivity extends FragmentActivity {

private Frag_01 frag_01;
private Frag_02 frag_02;
private Frag_03 frag_03;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    frag_01 = new Frag_01();
    frag_02 = new Frag_02();
    frag_03 = new Frag_03();
    transaction.add(R.id.frag_01, frag_01);
    transaction.add(R.id.frag_01, frag_02);
    transaction.add(R.id.frag_01, frag_03);
    transaction.hide(frag_03);
    transaction.hide(frag_02);
    transaction.commit();

    RadioGroup rg = findViewById(R.id.rg);
    rg.check(1);
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.hide(frag_01);
            transaction.hide(frag_02);
            transaction.hide(frag_03);

            switch(checkedId){
                case 1:
                    transaction.show(frag_01);
                    break;
                case 2:
                    transaction.show(frag_02);
                    break;
                case 3:
                    transaction.show(frag_03);
                    break;
            }
            transaction.commit();
        }
    });

}

}