简体   繁体   English

Android Studio 中的圆角和边框

[英]Rounded Corners and Borders in Android Studio

I want to add rounded corners and borders to a textview.我想向文本视图添加圆角和边框。 But only the top corners should be rounded and the bottom should be without border.但只有顶角应该是圆角,底部应该没有边框。 Already found this:已经找到这个:

https://www.android-examples.com/add-rounded-border-to-textview-programmatically/ https://www.android-examples.com/add-rounded-border-to-textview-programmatically/

But then I have rounded corners at the bottom too.但后来我在底部也有圆角。

How can I change this?我怎样才能改变这个?

Create a drawable file like this :创建一个可绘制的文件,如下所示:

    <?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="-4dp">

    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF" />
        <stroke android:width="4dp" android:color="#000000" />
        <corners android:radius="4dp" />
    </shape>

</inset>

And then apply it as an background of any control and there you go, it's done.然后将其应用为任何控件的背景,然后就完成了。

With the Material Components Library you can use the MaterialShapeDrawable to draw custom shapes .借助材料组件库,您可以使用MaterialShapeDrawable 绘制自定义形状

With a TextView you can do:使用 TextView,您可以执行以下操作:

<TextView
    android:id="@+id/tv_rounded"
    android:paddingLeft="8dp"
    ../>

Then create a MaterialShapeDrawable .然后创建一个MaterialShapeDrawable Something like:就像是:

    TextView textview = findViewById(R.id.tv_rounded);
    ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
        .toBuilder()
        .setAllCorners(CornerFamily.ROUNDED,radius)
        .setBottomRightCorner(CornerFamily.ROUNDED,0)
        .setBottomLeftCorner(CornerFamily.ROUNDED,0)
        .build();
    MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
    shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color.xxxx));
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.xxx));    
    ViewCompat.setBackground(textview,shapeDrawable);

在此处输入图片说明

Create Draweable rounded_border.xml创建 Draweable rounded_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <padding
        android:bottom="3dp"
        android:left="3dp"
        android:right="3dp"
        android:top="3dp" />

    <stroke
        android:width="1dp"
        android:color="#FFFFFF" />

    <corners
        android:radius="5dp" />

    <size
        android:width="110dp"
        android:height="110dp" />
</shape>

Then Set this Drawable in view's background property然后在视图的背景属性中设置此 Drawable

 <TextView
    android:id="@+id/tv_register"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_are_you_new_user"
    android:layout_margin="5dp"
    android:background="@drawable/rounded_border"
    android:fontFamily="@font/montserrat"
    android:gravity="center_horizontal"
    android:padding="10dp"
    android:text="Register"
    android:textAllCaps="true"
    android:textColor="@color/blue"
    android:textSize="15sp" />

Also helpful this solution to you: Click Me此解决方案对您也有帮助: 单击我

Hope this may help you希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM