我如何制作圆角布局?我想应用圆角到我的线性布局。
当前回答
我来晚了一点,但这仍然是个问题。因此,我为数据绑定编写了一组OutlineProviders和BindingAdapters,使您能够从xml中剪切角。
注意:剪裁与轮廓不支持角是不同的大小!
我在这个stackoverflow帖子上写了一个详细的回应
你会得到什么代码+绑定适配器:
<androidx.constraintlayout.widget.ConstraintLayout
clipRadius="@{@dimen/some_radius}"
clipBottomLeft="@{@dimen/some_radius}"
clipBottomRight="@{@dimen/some_radius}"
clipTopLeft="@{@dimen/some_radius}"
clipTopRight="@{@dimen/some_radius}"
clipCircle="@{@bool/clip}"
这使您可以将视图剪辑到一个圆,圆角所有角,圆角在一个方向(左,上,右,下)或单个角。
其他回答
如果你想让你的布局圆润,最好使用CardView,它提供了许多功能,使设计美观。
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:text="@string/quote_code"
android:textColor="@color/white"
android:textSize="@dimen/text_head_size" />
</LinearLayout>
</android.support.v7.widget.CardView>
使用这个card_view:cardCornerRadius="5dp",你可以改变半径。
我已经在@gauravsapiens的回答中给出了我的评论,让你对参数将产生的影响有一个合理的理解。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Background color -->
<solid android:color="@color/white" />
<!-- Stroke around the background, width and color -->
<stroke android:width="4dp" android:color="@color/drop_shadow"/>
<!-- The corners of the shape -->
<corners android:radius="4dp"/>
<!-- Padding for the background, e.g the Text inside a TextView will be
located differently -->
<padding android:left="10dp" android:right="10dp"
android:bottom="10dp" android:top="10dp" />
</shape>
如果你只是想创建一个圆角的形状,去掉填充和描边就可以了。如果你把固体也去掉,你会在透明的背景上创建圆角。
为了偷懒,我在下面创建了一个形状,它只是一个纯白色的圆角背景-享受!:)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Background color -->
<solid android:color="@color/white" />
<!-- The corners of the shape -->
<corners android:radius="4dp"/>
</shape>
我是这样做的:
检查屏幕截图:
在可绘制文件夹中创建名为custom_rectangle.xml的可绘制文件:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@android:color/white" />
<corners android:radius="10dip" />
<stroke
android:width="1dp"
android:color="@android:color/white" />
</shape>
现在在视图上应用矩形背景:
mView.setBackground(R.drawlable.custom_rectangle);
Done
我来晚了一点,但这仍然是个问题。因此,我为数据绑定编写了一组OutlineProviders和BindingAdapters,使您能够从xml中剪切角。
注意:剪裁与轮廓不支持角是不同的大小!
我在这个stackoverflow帖子上写了一个详细的回应
你会得到什么代码+绑定适配器:
<androidx.constraintlayout.widget.ConstraintLayout
clipRadius="@{@dimen/some_radius}"
clipBottomLeft="@{@dimen/some_radius}"
clipBottomRight="@{@dimen/some_radius}"
clipTopLeft="@{@dimen/some_radius}"
clipTopRight="@{@dimen/some_radius}"
clipCircle="@{@bool/clip}"
这使您可以将视图剪辑到一个圆,圆角所有角,圆角在一个方向(左,上,右,下)或单个角。
如果你想要的只是一个简单的圆角矩形,长话短说。
float r=8;
ShapeDrawable shape =
new ShapeDrawable (new RoundRectShape(new float[] { r, r, r, r, r, r, r, r },null,null));
shape.getPaint().setColor(Color.RED);
view.setBackground(shape);
前两个浮动用于左上角(其余对按顺时针方向对应)。
要了解更多细节,请阅读这个答案
推荐文章
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 点击下载Href图片链接
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)