android - 如何以编程方式设置圆角半径?
android - How to set corner radius programmatically?
我有两个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 中创建了一个新的布局,设置了右角半径,并用 setBackgroundRescource
将其设置到 textView,但仍然没有用。
我在两个 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>
这里是如何以编程方式创建 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);
@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()
我有两个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 中创建了一个新的布局,设置了右角半径,并用 setBackgroundRescource
将其设置到 textView,但仍然没有用。
我在两个 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>
这里是如何以编程方式创建 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);
@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()