我如何制作圆角布局?我想应用圆角到我的线性布局。
当前回答
一个更好的方法是:
background_activity.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:gravity="fill">
<color android:color="@color/black"/>
</item>
<item>
<shape android:gravity="fill">
<solid android:color="@color/white"/>
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
</item>
</layer-list>
这也将在API 21以下工作,并给你类似这样的东西:
如果您愿意多做一些努力,更好地控制,那么请使用android.support.v7.widget。使用CardView的cardCornerRadius属性(并将elevation属性设置为0dp以摆脱任何伴随的投影)。此外,这将从API水平低至15起工作。
其他回答
1:在drawables中定义layout_bg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dp" android:color="#B1BCBE" />
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
2:添加layout_bg.xml作为布局的背景
android:background="@drawable/layout_bg"
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dip" android:color="#B1BCBE" />
<corners android:radius="10dip"/>
<padding android:left="3dip" android:top="3dip" android:right="3dip" android:bottom="3dip" />
</shape>
@David,只是把padding的值和stroke一样,所以无论图像大小,边框都是可见的
我已经在@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>
使用CardView获得任何布局的圆角边缘。 使用card_view:cardCornerRadius="5dp"为cardview获得圆角布局边缘。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<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"
android:orientation="horizontal"
android:padding="15dp"
android:weightSum="1">
<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" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".7"
android:text="@string/quote_details"
android:textColor="@color/white"
android:textSize="@dimen/text_head_size" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
对于API 21+,使用剪辑视图
在API 21中,圆形轮廓剪辑被添加到View类中。更多信息请参见培训文档或参考资料。
这个内置的功能使圆角非常容易实现。它适用于任何视图或布局,并支持适当的剪辑。
下面是该怎么做:
创建一个可绘制的圆形形状,并将其设置为视图的背景: android:背景= " @drawable / round_outline” 在代码中剪辑轮廓:setClipToOutline(true)
文档曾经说过,你可以设置android:clipToOutline=“true”的XML,但这个错误现在终于解决了,文档现在正确地指出,你只能在代码中这样做。
它的样子:
关于ImageViews的特别说明
setClipToOutline()仅在视图的背景设置为可绘制的形状时才有效。如果此背景形状存在,视图将背景的轮廓作为剪切和阴影目的的边界。
这意味着如果你想圆角的ImageView与setClipToOutline(),你的图像必须来自android:src而不是android:background(因为背景是用于圆角形状)。如果你必须使用背景来设置你的图像而不是src,你可以使用这个嵌套视图的解决方案:
创建一个外部布局,将其背景设置为可绘制的形状 将布局包装在ImageView周围(没有填充) ImageView(包括布局中的任何其他内容)现在将被剪切到外部布局的圆形形状。
推荐文章
- 这是在Android中获取用户位置的好方法
- Android从左到右幻灯片动画
- 如何用OpenCV2.0和Python2.6调整图像大小
- 如何检索视图的维度?
- 如何改变菜单项的文本颜色在安卓?
- Android选择器和文本颜色
- 视图绑定-我如何获得包含布局的绑定?
- 在Android Studio中改变矢量资产的填充颜色
- 在构建中编写注释的语法是什么?gradle文件?
- 如何以编程方式添加按钮色调
- 用Android Studio进行调试永远停留在“等待调试器”状态
- Openssl不被视为内部或外部命令
- 无法执行dex:在Eclipse中超过GC开销限制
- 如何以编程方式将视图添加到视图
- 单击url会打开默认浏览器