02.jpg
前言
官网:MaterialButtonToggleGroup
继承关系
23.png
属性
24.png
效果图
01.png
布局文件
<?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=".MainActivity">
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_group"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="@dimen/DIMEN_12dp"
android:layout_marginRight="@dimen/DIMEN_12dp"
app:checkedButton="@id/mbtn_news"
app:singleSelection="true"
app:selectionRequired="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/mbtn_news"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius="10dp"
android:text="新闻"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/mbtn_funs"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="娱乐"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/mbtn_images"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius="10dp"
android:text="图片"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup2"
android:layout_width="wrap_content"
android:layout_height="@dimen/DIMEN_60dp"
android:layout_margin="@dimen/DIMEN_12dp"
app:checkedButton="@id/btnA"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btnA"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Android"
app:icon="@android:drawable/star_on" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnS"
style="@style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Sunny"
app:icon="@android:drawable/ic_lock_lock" />
</com.google.android.material.button.MaterialButtonToggleGroup>
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_group2"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="@dimen/DIMEN_12dp"
android:layout_marginRight="@dimen/DIMEN_12dp"
app:singleSelection="false">
<com.google.android.material.button.MaterialButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius="10dp"
android:text="新闻"/>
<com.google.android.material.button.MaterialButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="娱乐"/>
<com.google.android.material.button.MaterialButton
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius="10dp"
android:text="图片"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
</LinearLayout>
逻辑代码
public class MainActivity extends AppCompatActivity {
private MaterialButtonToggleGroup mToggleGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
initView();
initEvent();
}
private void initView() {
mToggleGroup = findViewById(R.id.toggle_group);
}
private void initEvent() {
mToggleGroup.addOnButtonCheckedListener(new MaterialButtonToggleGroup.OnButtonCheckedListener() {
@Override
public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
switch (checkedId) {
case R.id.mbtn_news:
Toast.makeText(MainActivity.this,"clicked new",Toast.LENGTH_SHORT).show();
break;
case R.id.mbtn_funs:
Toast.makeText(MainActivity.this,"clicked fun",Toast.LENGTH_SHORT).show();
break;
case R.id.mbtn_images:
Toast.makeText(MainActivity.this,"clicked image",Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
}
}
网友评论