我想向TextView添加圆角和边框。但是只有顶部角应该是圆角,底部应该没有边框。
已经找到了这个:
https://www.android-examples.com/add-rounded-border-to-textview-programmatically/
但这样底部也有圆角了。
怎么改变这个问题呢?
我想向TextView添加圆角和边框。但是只有顶部角应该是圆角,底部应该没有边框。
已经找到了这个:
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>
然后将它作为任何控件的背景应用,完成了。
MaterialShapeDrawable
来绘制自定义形状。<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);
创建可绘制的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" />
这个解决方案对你有帮助:点我
希望能对你有所帮助