【问题标题】:android - How to set corner radius programmatically?android - 如何以编程方式设置角半径?
【发布时间】:2017-05-19 13:47:13
【问题描述】:

我有两个ListViews(leftList,rightList)。我也有一个TextView,我用它作为他们两个中的行视图。 我有一个矩形可绘制形状并将其设置为TextView 的背景。

我想改变这个形状并在左边有圆角。

我尝试了什么:

       GradientDrawable gradientDrawable = new GradientDrawable();
       // gradientDrawable.setCornerRadius(30);
        ((GradientDrawable)gradientDrawable.mutate()).setCornerRadius(30);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            viewHolderPattern.digits.setBackground(gradientDrawable);
        }

我在 drawable 中创建了一个新布局,设置了右角半径并将其设置为 textView 并使用setBackgroundRescource,但仍然没有工作。

我在两个 listView 中用作项目的 TextView

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/digitsTextView"
    android:textSize="15dp"
    android:paddingTop="7dp"
    android:paddingBottom="8dp"
    android:fontFamily="monospace"
    android:textColor="@color/selected_items"
    android:background="@drawable/digital_text_shape">
</TextView>

形状布局 digital_text_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="@color/orange" />
    <solid android:color="@color/orange" />
    <corners
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="0dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="0dp"
        />
    <padding
        android:bottom="0dp"
        android:left="20dp"
        android:right="0dp"
        android:top="0dp" />
</shape>

左列表和右列表

<!-- Left ListView -->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                        <ListView
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_gravity="center_horizontal"
                            android:id="@+id/left_listView"
                            android:divider="@android:color/transparent"
                            android:dividerHeight="0.1sp"
                            android:choiceMode="singleChoice"

                            >
                        </ListView>
                </LinearLayout>

                <!-- Right ListView -->
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    >
                        <ListView
                            android:id="@+id/right_listView"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:divider="@android:color/transparent"
                            android:dividerHeight="0.1sp"
                            android:choiceMode="singleChoice"
                            >
                        </ListView>
                </LinearLayout>

【问题讨论】:

  • 你知道,对于 TextViews 你可以用 /> 来关闭它们。最后不需要第二个标签,除非里面有东西。
  • 为什么不让不同的viewholders使用不同的xml?回收者视图.. 使用列表视图来处理像这样的复杂列表似乎需要付出很多努力——回收者的目标是这些丰富、复杂的列表
  • 如果创建两个不同的 TextView 背景 xml 文件会怎样。一左一右。然后,如果您在左侧视图中:textView.setBackground(getResources().getDrawable(R.drawable.leftTextViewBackground));,在右侧列表视图中:textView.setBackground(getResources().getDrawable(R.drawable. rightTextViewBackground)); ?
  • @LunarWatcher 你是对的!感谢您提及!
  • @SaikCaskey 我已经有一个viewholder,并且我有我想要修改的textView,我听说过recycler view,我也会尝试,但是现在,我想做这与列表。

标签: android listview textview rounded-corners


【解决方案1】:

这里是如何以编程方式创建GradientDrawable 形状的示例。

GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.RED);
shape.setStroke(3, Color.YELLOW);

用于更改渐变所有角的半径。

shape.setCornerRadius(15);

用于更改渐变特定角的半径。

shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });

您可以使用此可绘制对象作为背景,如下所示:

view.setBackgroundDrawable(shape);

【讨论】:

  • {mTopLeftRadius, mTopLeftRadius, mTopRightRadius, mTopRightRadius, mBottomRightRadius, mBottomRightRadius, mBottomLeftRadius, mBottomLeftRadius}
  • 在可绘制对象上调用 .mutate() 如果它在其他地方使用以防止这些实例也发生变化
【解决方案2】:

@Rohit Suthar 在 Kotlin 中的回答,将 dp 转换为像素,以及颜色资源:

private val STROKE_WIDTH_PX = 2.dpToPx
private val CORNER_RADIUS_PX = 5.dpToPx.toFloat()


val shape = GradientDrawable()
shape.shape = GradientDrawable.RECTANGLE
shape.setColor(ContextCompat.getColor(context, R.color.pink))
shape.setStroke(STROKE_WIDTH_PX, ContextCompat.getColor(context, R.color.black))
// choose either cornerRadius or cornerRadii
shape.cornerRadius = CORNER_RADIUS_PX
shape.cornerRadii = floatArrayOf(
    // top left
    0f,
    0f,
    // top right
    CORNER_RADIUS_PX,
    CORNER_RADIUS_PX,
    // bottom right
    CORNER_RADIUS_PX,
    CORNER_RADIUS_PX,
    // bottom left
    0f,
    0f
)
view.background = shape

将 dp 转换为 px:

val Int.dpToPx: Int
    get() = (this * Resources.getSystem().displayMetrics.density).toInt()

【讨论】:

    猜你喜欢
    • 2016-01-07
    • 1970-01-01
    • 2017-08-19
    • 1970-01-01
    • 2019-04-13
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多