public class MainActivity extends AppCompatActivity { private RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = findViewById(R.id.group); initData(); } private void initData() { String[] names = {"全部", "苹果", "梨", "香蕉", "猕猴桃", "枇杷", "油桃", "荔枝", "哈密瓜","葡萄","酸枣","枣泥","核桃","菠萝","橘子","橙子"}; for (int i = 0; i < names.length; i++) { RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.item_radiobutton, null, false); radioButton.setId(i); radioButton.setText(names[i]); radioGroup.addView(radioButton); if (0 == i) { radioGroup.check(0); } } radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int i) { RadioButton rb = radioGroup.findViewById(i); Toast.makeText(MainActivity.this, rb.getId() + "", Toast.LENGTH_SHORT).show(); } }); } // @Override // public void onWindowFocusChanged(boolean hasFocus) { // super.onWindowFocusChanged(hasFocus); // if (hasFocus && Build.VERSION.SDK_INT >= 19) { // View decorView = getWindow().getDecorView(); // decorView.setSystemUiVisibility( // View.SYSTEM_UI_FLAG_LAYOUT_STABLE // | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION // | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN // | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // | View.SYSTEM_UI_FLAG_FULLSCREEN // | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); // } // }
}
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.demo1.MainActivity"> <HorizontalScrollView android:layout_width="match_parent" android:scrollbars="none" android:layout_height="wrap_content"> <RadioGroup android:id="@+id/group" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#22aa33" android:orientation="horizontal" /> </HorizontalScrollView> </LinearLayout>
item_radiobutton布局:
<?xml version="1.0" encoding="utf-8"?> <RadioButton xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/rb_bg_selector" android:button="@null" android:gravity="center" android:padding="8dp" android:text="测试" android:textColor="@color/radiobutton_text_color_selector" />
rb_bg_selector选中背景色:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true"> <layer-list> <!--底层使用蓝色填充色--> <!--stroke设置边框线,bottom,top取框线负值,背景图填充满--> <item android:bottom="-8dp" android:top="-8dp"> <shape> <solid android:color="#FF4081" /> <stroke android:width="8dp" android:color="#FFFFFF" /> </shape> </item> <!-- 上面一层距离底层的顶部1dp,距离底部1dp,类似marginTop,填充色为白色,这样就形成了一个带有蓝色顶部边线和底部边线的白色背景的图--> <item android:bottom="1dp"> <shape> <solid android:color="#FFFFFF" /> <padding android:left="8dp" android:right="8dp" /> </shape> </item> </layer-list> </item> <item> <shape> <solid android:color="#FFFFFF" /> </shape> </item> </selector>文字选中背景色:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/colorAccent" android:state_checked="true" /> <item android:color="@color/colorPrimary"/> </selector>