[英]Rounded corner for textview in android
我有一个 textview,希望它的角是圆形的。 我已经知道可以使用android:background="@drawable/somefile"
来完成。 就我而言,此标签已包含在内,因此无法再次使用。 例如android:background="@drawable/mydialogbox"
已经在那里创建背景图像
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:background="@drawable/mydialogbox"
android:orientation="horizontal" >
<TextView
android:id="@+id/textview_name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</RelativeLayout>
所以当我想要textview(textview_name)
也有圆角时,这是如何实现的。
在drawable
文件夹中创建rounded_corner.xml
并添加以下内容,
<solid android:color="#ffffff" /> <padding android:left="1dp" android:right="1dp" android:bottom="1dp" android:top="1dp" /> <corners android:radius="5dp" />
在TextView
背景属性中设置此可绘制对象,如下所示:
android:background="@drawable/rounded_corner"
我希望这对你有用。
除了radius
之外,还有一些属性可以用于圆角,如topRightRadius
, topLeftRadius
, bottomRightRadius
, bottomLeftRadius
带有带角和gray
背景的red
边框的示例TextView
bg_rounded.xml (在 drawables 文件夹中)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="10dp"
android:color="#f00" />
<solid android:color="#aaa" />
<corners
android:radius="5dp"
android:topRightRadius="100dp" />
</shape>
文本视图
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_rounded"
android:text="Text"
android:padding="20dp"
android:layout_margin="10dp"
/>
结果
由于您的顶级视图已经设置了 android:background 属性,您可以使用<layer-list>
( link ) 创建一个新的 XML drawable,它结合了旧背景和新圆角背景。
列表中的每个<item>
元素都绘制在下一个元素上,因此列表中的最后一个元素是位于顶部的元素。
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap android:src="@drawable/mydialogbox" />
</item>
<item>
<shape>
<stroke
android:width="1dp"
android:color="@color/common_border_color" />
<solid android:color="#ffffff" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
通过 Material Components Library,您可以使用MaterialShapeDrawable
。
使用TextView
:
<TextView
android:id="@+id/textview"
../>
您可以以编程方式应用MaterialShapeDrawable
:
float radius = getResources().getDimension(R.dimen.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);
如果要更改背景颜色和边框,只需应用:
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.....));
shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color....));
有两个步骤
1)在你的drawable文件夹中创建这个文件:- rounded_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp" /> // set radius of corner
<stroke android:width="2dp" android:color="#ff3478" /> // set color and width of border
<solid android:color="#FFFFFF" /> // inner bgcolor
</shape>
2) 将此文件设置为您的TextView
作为背景属性。
android:background="@drawable/rounded_corner"
您也可以在 Button 或 Edittext 中使用此 drawable
在 drawable 文件夹下创建一个 xml gradient.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<corners android:radius="50dip" />
<stroke android:width="1dip" android:color="#667162" />
<gradient android:angle="-90" android:startColor="#ffffff" android:endColor="#ffffff" />
</shape>
</item>
</selector>
然后将此添加到您的 TextView
android:background="@drawable/gradient"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<stroke android:width="1dp" />
<solid android:color="#1e90ff" />
</shape>
android:background="@drawable/corner"
您可以使用提供的矩形形状(没有渐变,除非您想要一个),如下所示:
在drawable/rounded_rectangle.xml
:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp" />
<stroke android:width="1dp" android:color="#ff0000" />
<solid android:color="#00ff00" />
</shape>
然后在您的文本视图中:
android:background="@drawable/rounded_rectangle"
当然,您需要自定义尺寸和颜色。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="5dp" />
<solid android:color="#ffffff"/>
</shape>
</item>
</layer-list>
<com.google.android.material.card.MaterialCardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardCornerRadius="25dp"
>
<com.google.android.material.textview.MaterialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/emergency_sms_template"
android:padding="6dp"
android:textSize="11sp"
/>
</com.google.android.material.card.MaterialCardView>
您可以使用 SVG 进行圆角并加载到 ImageView 并使用 ConstraintLayout 将 ImageView 带入 TextView
我将它用于圆角 ImageView 和圆角 TextView
只需使用圆角图像作为该视图的背景
并且不要忘记将您的自定义图像放在 drawable 文件夹中
android:background="@drawable/my_custom_image"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.