Android Studio 中的圆角和边框

Rounded Corners and Borders in Android Studio

我想给文本视图添加圆角和边框。但只有顶角应该是圆的,底部应该没有边框。 已经找到这个:

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

但是我的底部也有圆角。

我该如何更改?

像这样创建可绘制文件:

    <?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>

然后将其应用为任何控件的背景,就这样,大功告成。

借助 Material 组件库,您可以使用 MaterialShapeDrawable to draw custom shapes

使用 TextView 您可以:

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

然后创建一个MaterialShapeDrawable。类似于:

    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);

创建 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>

然后在视图的背景中设置此 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" />

此解决方案对您也有帮助:

希望对您有所帮助