Android: Is there a simple way to create rounded corners for a view without having to create a separate drawable each time?

回眸只為那壹抹淺笑 提交于 2020-08-25 21:17:01

问题


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.


回答1:


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.




回答2:


If you just want to change the color of the background, you can use backgroundTint

For example:


    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="@drawable/background"
        app:backgroundTint="#F00"
        />

Please note: I'm using app:backgroundTint here, by using app prefix, AppCompat Views (AppCompatImageView, AppCompatButton, etc) can read this attribute and tint the background on pre-lollipop Android devices, but you should make sure you are using appcompat library.

The universal solution:

On second thought, I come up with a method: you can create a LayoutInflater.Factory and replace all the views(imageView, TextView, etc) with your own custom view which takes corner radius and color as background, just like AppCompat library.




回答3:


You can use Gradient drawable for your views programmatically and can set that view as background

 GradientDrawable getRoundedCornerView(int shapetype,int color,float radius){
     GradientDrawable shape = new GradientDrawable();
     shape.setShape(shapetype); //GradientDrawable.RECTANGLE for rectangle
     shape.setColor(color);
     shape.setCornerRadius(radius);
retrun shape;
}


来源:https://stackoverflow.com/questions/59046711/android-is-there-a-simple-way-to-create-rounded-corners-for-a-view-without-havi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!