我想把纽扣的角弄圆。在Android中是否有一种简单的方法来实现这一点?
当前回答
我发现的简单方法是在可绘制文件夹中制作一个新的xml文件,然后将按钮背景指向该xml文件。下面是我使用的代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff8100"/>
<corners android:radius="5dp"/>
</shape>
其他回答
这是一个简单的CardView使用这个你可以让你的按钮的角是圆的。
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="30dp"
android:background="#fff"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<Button
android:id="@+id/button_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textSize="20sp"
app:cornerRadius="32dp"/>
</androidx.cardview.widget.CardView>
在Android中是否有一种简单的方法来实现这一点?
使用Jetpack Compose,你可以使用shape参数:
Button(
onClick = { /* Do something! */ },
shape = RoundedCornerShape(8.dp)
){
Text("Button")
}
在Material Components库中,你可以在app:cornerRadius属性中使用MaterialButton。
喜欢的东西:
<com.google.android.material.button.MaterialButton
android:text="BUTTON"
app:cornerRadius="8dp"
../>
得到一个圆角按钮就足够了。
你可以使用一种材质按钮样式。 例如:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
.../>
从1.1.0版本开始,你还可以改变按钮的形状。只需在按钮样式中使用shapeAppearanceOverlay属性:
<style name="MyButtonStyle" parent="Widget.MaterialComponents.Button">
<item name="shapeAppearanceOverlay">@style/ShapeAppearanceOverlay.MyApp.Button.Rounded</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">16dp</item>
</style>
然后使用:
<com.google.android.material.button.MaterialButton
style="@style/MyButtonStyle"
.../>
你也可以在xml布局中应用shapeAppearanceOverlay:
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
.../>
shapeAppearance还允许每个角有不同的形状和尺寸:
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSizeTopLeft">32dp</item>
<item name="cornerSizeBottomLeft">32dp</item>
</style>
如果您使用的是矢量可绘制对象,那么您只需要在可绘制对象定义中指定一个<corners>元素。我在一篇博客文章中提到了这一点。
如果你使用位图/ 9-patch绘制,那么你需要在位图图像中创建透明的角落。
我发现的简单方法是在可绘制文件夹中制作一个新的xml文件,然后将按钮背景指向该xml文件。下面是我使用的代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#ff8100"/>
<corners android:radius="5dp"/>
</shape>
创建可绘制的XML文件,并将按钮背景设置为此文件。
XML文件代码,例如:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--|^@^|_[ Shadows ]_|^@^|-->
<item>
<shape>
<padding android:top="2dp" android:right="2dp" android:bottom="2dp" android:left="2dp" />
<gradient android:angle="315" android:startColor="#c2c2c2" android:endColor="#c0c0c0"/>
<corners android:radius="3dp" />
</shape>
</item>
<!--|^@^|_[ Background ]_|^@^|-->
<item>
<shape>
<gradient android:angle="135" android:startColor="#f7f7f7" android:endColor="#fbfcfc"/>
<corners android:radius="3dp" />
</shape>
</item>
</layer-list>
推荐文章
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件
- 修改抽射超时时间
- 如何通过数据从第二个活动到第一个活动时按回?——安卓
- 如何在android中获得当前前景活动上下文?
- 如何在Android中获取当前日期?
- 获取Android设备名称