首先是RadioButton的背景selector
左边的selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<corners
android:topLeftRadius="5dp"
android:bottomLeftRadius="5dp"/>
<solid android:color="@color/green"/>
</shape>
</item>
<item>
<shape>
<corners
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp" />
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="@color/green" />
</shape>
</item>
</selector>
中间的selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<solid android:color="@color/green"/>
</shape>
</item>
<item>
<shape>
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="@color/green" />
</shape>
</item>
</selector>
右边的selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<shape>
<corners
android:topRightRadius="5dp"
android:bottomRightRadius="5dp"/>
<solid android:color="@color/green"/>
</shape>
</item>
<item>
<shape>
<corners
android:bottomRightRadius="5dp"
android:topRightRadius="5dp" />
<solid android:color="@color/white" />
<stroke
android:width="1dp"
android:color="@color/green" />
</shape>
</item>
</selector>
字体的selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/white" android:state_checked="true"/>
<item android:color="@color/white" android:state_focused="true"/>
<item android:color="@color/white" android:state_pressed="true"/>
<item android:color="@color/green"/>
</selector>
activity_main.xml ,上面是三个RadioButton,下面是button对应的layout
<?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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkedButton="@+id/rb_train"
android:gravity="center"
android:padding="10dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb_train"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/sel_radio_left"
android:button="@null"
android:gravity="center"
android:padding="10dp"
android:text="培训"
android:textColor="@drawable/sel_radio_text"
android:textSize="12sp" />
<RadioButton
android:id="@+id/rb_evaluate"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/sel_radio_mid"
android:button="@null"
android:gravity="center"
android:padding="10dp"
android:text="评测"
android:textColor="@drawable/sel_radio_text"
android:textSize="12sp" />
<RadioButton
android:id="@+id/rb_examRecord"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:background="@drawable/sel_radio_right"
android:button="@null"
android:gravity="center"
android:padding="10dp"
android:text="考试记录"
android:textColor="@drawable/sel_radio_text"
android:textSize="12sp" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/ll_train"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="培训 " />
<ListView
android:id="@+id/lv_train"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_evaluate"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="评测 " />
<ListView
android:id="@+id/lv_evaluate"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_examRecord"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="考试记录 " />
<ListView
android:id="@+id/lv_examRecord"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
MainActivity
package com.lzz.demoapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
private RadioGroup rg;
private LinearLayout ll_train,ll_evaluate,ll_examRecord;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rg = (RadioGroup) findViewById(R.id.rg);
rg.setOnCheckedChangeListener(this);
ll_train = (LinearLayout) findViewById(R.id.ll_train);
ll_evaluate = (LinearLayout) findViewById(R.id.ll_evaluate);
ll_examRecord = (LinearLayout) findViewById(R.id.ll_examRecord);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
if (checkedId == R.id.rb_train) {
clickTrain();
} else if (checkedId == R.id.rb_evaluate) {
clickEvaluate();
} else if (checkedId == R.id.rb_examRecord) {
clickExamRecord();
}
}
private void clickExamRecord() {
ll_train.setVisibility(View.GONE);
ll_evaluate.setVisibility(View.GONE);
ll_examRecord.setVisibility(View.VISIBLE);
}
private void clickEvaluate() {
ll_train.setVisibility(View.GONE);
ll_evaluate.setVisibility(View.VISIBLE);
ll_examRecord.setVisibility(View.GONE);
}
private void clickTrain() {
ll_train.setVisibility(View.VISIBLE);
ll_evaluate.setVisibility(View.GONE);
ll_examRecord.setVisibility(View.GONE);
}
}
以上。