-
效果图
-
资源引入
implementation 'com.google.android.material:material:1.4.0'
- 属性
Chip 属性 | 描述 |
---|---|
android:id | 控件id |
style样式属性 | 系统默认4种 1.@style/Widget.MaterialComponents.Chip.Entry 2.@style/Widget.MaterialComponents.Chip.Choice 3.@style/Widget.MaterialComponents.Chip.Action 4.@style/Widget.MaterialComponents.Chip.Filter |
android:layout_width | 控件长度 |
android:layout_height | 控件高度 |
android:text | 文本内容 |
android:textColor | 文本颜色 |
app:chipBackgroundColor | 背景颜色 |
app:chipIcon | 文本内容前的icon |
app:closeIcon | 文本内容后的icon |
app:chipIconVisible | 文本内容前的icon是否可见 |
app:closeIconVisible | 文本内容后的icon是否可见 |
app:chipIconSize | 文本内容前的icon大小 |
app:closeIconSize | 文本内容后的icon大小 |
app:checkedIcon | 选中时的icon |
app:chipIconTint | 文本内容前的icon颜色着色器 |
app:closeIconTint | 文本内容后的icon颜色着色器 |
ChipGroup 属性 | 描述 |
---|---|
android:id | 控件id |
android:layout_width | 控件长度 |
android:layout_height | 控件高度 |
app:singleSelection | 是否单选 |
app:singleLine | 是否单行显示 |
- 源代码
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:singleSelection="false">
<com.google.android.material.chip.Chip
android:id="@+id/chip1"
style="@style/Widget.MaterialComponents.Chip.Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAA"
android:textColor="@color/white"
app:chipBackgroundColor="@color/teal_200"
app:chipIcon="@drawable/ic_baseline_videocam_24"
app:chipIconVisible="true" />
<com.google.android.material.chip.Chip
android:id="@+id/chip2"
style="@style/Widget.MaterialComponents.Chip.Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BBBCCCCCCCC"
android:textColor="@color/white"
app:chipBackgroundColor="@color/teal_200"
app:chipIcon="@drawable/ic_baseline_mic_24"
app:closeIconVisible="true" />
<com.google.android.material.chip.Chip
android:id="@+id/chip3"
style="@style/Widget.MaterialComponents.Chip.Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CCC"
android:textColor="@color/white"
app:chipBackgroundColor="@color/teal_200" />
<com.google.android.material.chip.Chip
android:id="@+id/chip4"
style="@style/Widget.MaterialComponents.Chip.Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DDDDSSSSSSSS"
android:textColor="@color/white"
app:chipBackgroundColor="@color/teal_200"
app:chipIcon="@drawable/ic_baseline_mic_24" />
<com.google.android.material.chip.Chip
android:id="@+id/chip5"
style="@style/Widget.MaterialComponents.Chip.Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EEEASSSSS"
android:textColor="@color/white"
app:chipBackgroundColor="@color/teal_200"
app:chipIcon="@drawable/ic_baseline_mic_24" />
<com.google.android.material.chip.Chip
android:id="@+id/chip6"
style="@style/Widget.MaterialComponents.Chip.Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FFF"
android:textColor="@color/white"
app:chipBackgroundColor="@color/teal_200" />
<com.google.android.material.chip.Chip
android:id="@+id/chip7"
style="@style/Widget.MaterialComponents.Chip.Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GGGVVVVVV"
android:textColor="@color/white"
app:chipBackgroundColor="@color/teal_200" />
<com.google.android.material.chip.Chip
android:id="@+id/chip8"
style="@style/Widget.MaterialComponents.Chip.Entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GGGAAAAAAAAA"
android:textColor="@color/white"
app:chipBackgroundColor="@color/teal_200" />
</com.google.android.material.chip.ChipGroup>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavMenu"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
app:itemIconSize="25dp"
app:itemIconTint="@color/icon_color"
app:itemTextColor="@color/text_color"
app:labelVisibilityMode="labeled"
app:menu="@menu/bottom_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
activity
package com.yyf.demo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.google.android.material.button.MaterialButtonToggleGroup;
import com.google.android.material.chip.ChipGroup;
import com.google.android.material.navigation.NavigationBarView;
import com.yyf.demo.databinding.ActivityBottomAppBarBinding;
import java.util.ArrayList;
import java.util.List;
public class BottomAppBar extends AppCompatActivity {
private ActivityBottomAppBarBinding binding;
private static final String TAG = "BottomAppBar";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityBottomAppBarBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.bottomNavMenu.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Log.d(TAG, "onNavigationItemSelected: " + item.getTitle());
return true;
}
});
//只有当ChipGroup singleSelection=false时 该监听才会正常响应
binding.chipGroup.setOnCheckedChangeListener(new ChipGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(ChipGroup group, int checkedId) {
Log.d(TAG, "onCheckedChanged: " + checkedId);
}
});
//Chip点击选中事件
binding.chip1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "onCheckedChanged: " + isChecked);
}
});
//文本内容后面的icon点击响应事件
binding.chip1.setOnCloseIconClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "onClick: ");
}
});
}
}
- 注意事项
已在activity源码中注释,请留意