android - 如何以编程方式设置角半径?

P.L*_*and 5 android listview rounded-corners textview

我有两个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);
        }
Run Code Online (Sandbox Code Playgroud)

我已经在drawable中创建了一个新的布局,右边的半径设置并将其设置为textView setBackgroundRescource但仍然无效.

我在两个listViews中用作项目的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>
Run Code Online (Sandbox Code Playgroud)

形状布局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>
Run Code Online (Sandbox Code Playgroud)

左侧列表和右侧列表

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

Roh*_*har 14

这里以GradientDrawable编程方式创建形状的示例.

GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setColor(Color.RED);
shape.setStroke(3, Color.YELLOW);
Run Code Online (Sandbox Code Playgroud)

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

shape.setCornerRadius(15);
Run Code Online (Sandbox Code Playgroud)

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

shape.setCornerRadii(new float[] { 8, 8, 8, 8, 0, 0, 0, 0 });
Run Code Online (Sandbox Code Playgroud)

您可以将此drawable用作背景,如下所示:

view.setBackgroundDrawable(shape);
Run Code Online (Sandbox Code Playgroud)

  • {mTopLeftRadius,mTopLeftRadius,mTopRightRadius,mTopRightRadius,mBottomRightRadius,mBottomRightRadius,mBottomLeftRadius,mBottomLeftRadius} (12认同)

Alb*_*lvo 5

@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
Run Code Online (Sandbox Code Playgroud)

要将 dp 转换为 px:

val Int.dpToPx: Int
    get() = (this * Resources.getSystem().displayMetrics.density).toInt()
Run Code Online (Sandbox Code Playgroud)