将material3中点击底部导航栏的item的有色背景展开效果运用到image button中
时间: 2024-05-06 10:19:32 浏览: 11
您可以通过以下步骤将material3中点击底部导航栏的item的有色背景展开效果运用到image button中:
1. 在您的项目中添加material3库。
2. 在您的布局文件中添加一个image button,并为其设置点击监听器。
3. 在点击监听器中,使用以下代码来实现点击时展开有色背景效果:
```
ImageButton imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable();
shapeDrawable.setFillColor(ContextCompat.getColorStateList(MainActivity.this, R.color.colorAccent));
shapeDrawable.setStroke(4, Color.WHITE);
shapeDrawable.setShapeAppearanceModel(ShapeAppearanceModel.builder()
.setAllCorners(new RoundedCornerTreatment())
.build());
ViewCompat.setBackground(imageButton, shapeDrawable);
imageButton.postDelayed(new Runnable() {
@Override
public void run() {
MaterialShapeDrawable maskDrawable = new MaterialShapeDrawable();
maskDrawable.setFillColor(Color.WHITE);
maskDrawable.setStroke(4, Color.WHITE);
maskDrawable.setShapeAppearanceModel(ShapeAppearanceModel.builder()
.setAllCorners(new RoundedCornerTreatment())
.build());
ViewCompat.setBackground(imageButton, maskDrawable);
}
}, 200);
}
});
```
这段代码会在image button被点击时,创建一个有色背景的MaterialShapeDrawable对象,并将其设置为image button的背景。然后,设置一个延迟,在200ms后将背景重置为白色。
4. 运行您的应用程序,点击image button,您将看到有色背景的展开效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)