【问题标题】:Rounded corner for textview in androidandroid中textview的圆角
【发布时间】:2013-09-17 21:08:28
【问题描述】:

我有一个 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) 也带有圆角时,如何实现。

【问题讨论】:

标签: android textview shapes android-shapedrawable


【解决方案1】:
  1. drawable文件夹中创建rounded_corner.xml并添加如下内容,

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >         
       <stroke
           android:width="1dp"
           android:color="@color/common_border_color" />
    
       <solid android:color="#ffffff" />
    
       <padding
            android:left="1dp"
            android:right="1dp"
            android:bottom="1dp"
            android:top="1dp" />
    
       <corners android:radius="5dp" />
    </shape>
    
  2. TextView 背景属性中设置这个drawable,如下所示:

    android:background="@drawable/rounded_corner"
    

我希望这对你有用。

【讨论】:

  • 答案是对的,只是发帖的人没有详细解释。您需要创建一个 xml [例如。 rounded_view.xml] 在您的可绘制文件夹中使用上述代码。并在您的 textview 周围的布局中将其作为参数 android:background="@drawable/rounded_view"
  • android:background="@drawable/rounded_corner" 不要在这里使用扩展!
  • 如果对您不起作用,请添加 android:shape="rectangle"
  • 如果项目无法自动运行,则重新构建您的项目
  • xml代码应该用&lt;shape xmlns:android="http://schemas.android.com/apk/res/android"&gt;括起来
【解决方案2】:

除了radius,还有topRightRadiustopLeftRadiusbottomRightRadiusbottomLeftRadius等属性转角

示例TextView 带有带角的red 边框和gray 背景

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

结果

【讨论】:

    【解决方案3】:

    借助材料组件库,您可以使用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....));
    

    【讨论】:

    • 希望您已正确定义 R.dimen.corner_radius 以确保完整性。
    • 我未能在 RecyclerView 适配器中使用它。
    • @nyxee 这只是一个维度(如 8dp)。
    • 很棒的答案! @nyxee textViewcorner_radius 只是一个浮点数;设置您想要的任何值:float radius = 12f;例如。
    【解决方案4】:

    由于您的顶级视图已经设置了 android:background 属性,您可以使用 &lt;layer-list&gt; (link) 创建一个新的 XML 可绘制对象,该可绘制对象结合了您的旧背景和新的圆角背景。

    列表中的每个&lt;item&gt; 元素都被绘制在下一个元素之上,因此列表中的最后一项是最后一项。

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

    【讨论】:

      【解决方案5】:

      有两个步骤

      1) 在您的可绘制文件夹中创建此文件:- 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 中使用此可绘制对象

      【讨论】:

        【解决方案6】:

        您可以使用提供的矩形形状(没有渐变,除非您想要一个),如下所示:

        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"
        

        当然,您需要自定义尺寸和颜色。

        【讨论】:

        • 请问如何添加渐变
        【解决方案7】:

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

        【讨论】:

          【解决方案8】:
          1. 右键单击Drawable文件夹并创建新文件
          2. 根据您的要求命名文件并将扩展名添加为 .xml
          3. 在文件中添加如下代码
            <?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>
          
          1. 在您想要圆角边缘的位置添加行android:background="@drawable/corner"

          【讨论】:

            【解决方案9】:
            <?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>
            

            【讨论】:

              【解决方案10】:

              您可以使用 SVG 进行圆角并加载到 ImageView 并使用 ConstraintLayout 将 ImageView 带到 TextView 上

              我将它用于圆角 ImageView 和圆角 TextView

              【讨论】:

                【解决方案11】:

                只需使用圆角图像作为该视图的背景

                别忘了把你的自定义图片放在drawable文件夹中

                android:background="@drawable/my_custom_image"
                

                【讨论】:

                  猜你喜欢
                  • 2016-03-30
                  • 1970-01-01
                  • 2020-12-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-08-01
                  • 2013-10-28
                  相关资源
                  最近更新 更多