Android:是否有一种简单的方法可以为视图创建圆角而不必每次都创建单独的可绘制对象? [英] Android: Is there a simple way to create rounded corners for a view without having to create a separate drawable each time?

查看:62
本文介绍了Android:是否有一种简单的方法可以为视图创建圆角而不必每次都创建单独的可绘制对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上经历了各种解决方案,这些解决方案使我们能够创建带有圆角的视图.大多数情况下,每次需要圆角视图时,它们都需要使用创建自定义视图,或者使用xml或九个修补程序来创建可绘制对象.

问题在于,当我实现这样的视图时,即使两个视图除了背景颜色是相同的,我都需要为每个这样的视图创建一个可绘制的对象.这对我来说很烦人,我听说iOS框架提供了一种创建圆角视图的好方法.任何帮助将不胜感激.

除圆角外,视图和阴影的按效果也是常用的样式之一.请让您的解决方案包括这些影响.

解决方案

使用Material Components Library,您可以使用

使用简单的 查看 :

 <视图android:id ="@ + id/line"android:layout_width ="match_parent"android:layout_height="4dp"android:backgroundTint ="@ color/..."/& gt; 

然后应用相同的 MaterialShapeDrawable :

 查看行= findViewById(R.id.line);ViewCompat.setBackground(line,shapeDrawable); 

您还可以创建不同的角点:

  ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel().toBuilder().setAllCorners(CornerFamily.ROUNDED,0).setBottomRightCorner(CornerFamily.ROUNDED,radius).建造(); 

材质组件库提供的大多数组件都具有MaterialShapeDrawable作为背景.
在这些情况下,只需使用类似(在本示例中为MaterialCardView).

  MaterialCardView cardView = findViewById(R.id.card);cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel().toBuilder().setBottomLeftCornerSize(...).setBottomEdge(...).建造()); 

它需要库的版本 1.1.0 .当前为 1.1.0-beta02 .

I have gone through various solutions on the internet, which enable us to create views with rounded corners. Most of them require the use of creating custom views, or to create a drawable in xml or a nine-patch each time we need a rounded corner view.

The problem is that when I implement such views, I need to create a drawable for every such view, even if two views have everything but just the background color in common. This is kind of irritating for me and I've heard the iOS framework provides a good way to create round cornered views. Any help would be much appreciated.

Edit: Along with rounded corners, the press effect of a view and shadows are also among the common styles used. Please let your solution include those effects.

解决方案

With the Material Components Library you can use the MaterialShapeDrawable to draw custom shapes.

For example with a TextView you can do:

    <TextView
        android:id="@+id/textview"
        android:backgroundTint="@color/secondaryColor"
        ../>

Then create a MaterialShapeDrawable:

float radius = getResources().getDimension(R.dimen.default_corner_radius);

TextView textView = findViewById(R.id.textview);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED,radius)
        .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
ViewCompat.setBackground(textView,shapeDrawable);

With a simple View:

<View
    android:id="@+id/line"
    android:layout_width="match_parent"
    android:layout_height="4dp"
    android:backgroundTint="@color/..."/>

Then apply the same MaterialShapeDrawable:

View line = findViewById(R.id.line);
ViewCompat.setBackground(line,shapeDrawable);

You can also create different corners:

ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(CornerFamily.ROUNDED,0)
    .setBottomRightCorner(CornerFamily.ROUNDED,radius)
    .build();

Also most of the components provided by the Material Component Library have a MaterialShapeDrawable as background.
In these cases just use something like (in this example a MaterialCardView).

  MaterialCardView cardView = findViewById(R.id.card);
  cardView.setShapeAppearanceModel(cardView.getShapeAppearanceModel()
        .toBuilder()
        .setBottomLeftCornerSize(...)
        .setBottomEdge(...)
        .build());

It requires the version 1.1.0 of the library. Currently 1.1.0-beta02.

这篇关于Android:是否有一种简单的方法可以为视图创建圆角而不必每次都创建单独的可绘制对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆