Rounded corner for textview in android

前端 未结 11 1137
猫巷女王i
猫巷女王i 2020-12-02 04:30

I have a textview and want its corner to be in round shape. I already know it can be done using android:background=\"@drawable/somefile\". In my case, this tag

相关标签:
11条回答
  • 2020-12-02 04:43
    1. Create rounded_corner.xml in the drawable folder and add the following content,

      <solid android:color="#ffffff" />
      
      <padding
              android:left="1dp"
              android:right="1dp"
              android:bottom="1dp"
              android:top="1dp" />
      
      <corners android:radius="5dp" />
      
    2. Set this drawable in the TextView background property like so:

      android:background="@drawable/rounded_corner"
      

    I hope this is useful for you.

    0 讨论(0)
  • 2020-12-02 04:43

    There is Two steps

    1) Create this file in your drawable folder :- 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) Set this file into your TextView as background property.

    android:background="@drawable/rounded_corner"
    

    You can use this drawable in Button or Edittext also

    0 讨论(0)
  • 2020-12-02 04:44

    You can use SVG for rounding corners and load into an ImageView and use ConstraintLayout to bring ImageView on TextView

    I used it for rounded ImageView and rounded TextView

    0 讨论(0)
  • 2020-12-02 04:46

    With the Material Components Library you can use the MaterialShapeDrawable.

    With a TextView:

        <TextView
            android:id="@+id/textview"
            ../>
    

    You can programmatically apply a 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);
    

    If you want to change the background color and the border just apply:

    shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,R.color.....));
    shapeDrawable.setStroke(2.0f, ContextCompat.getColor(this,R.color....));
    
    0 讨论(0)
  • 2020-12-02 04:48

    Beside radius, there are some property to round corner like topRightRadius, topLeftRadius, bottomRightRadius, bottomLeftRadius

    Example TextView with red border with corner and gray background

    bg_rounded.xml (in the drawables folder)

    <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

    <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"
        />
    

    Result

    0 讨论(0)
  • 2020-12-02 04:49

    create an xml gradient.xml file under drawable folder

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

    then add this to your TextView

    android:background="@drawable/gradient"
    
    0 讨论(0)
提交回复
热议问题