如何在Android上设置按钮上的图像并通过xml添加圆形边缘?
我在活动中找到了几种解决方案,但我更愿意坚持使用xml进行布局和图形 我想在按钮上设置一个带有圆角的图像,而不是普通的默认角 XML示例:如何在Android上设置按钮上的图像并通过xml添加圆形边缘?,android,xml,image,button,Android,Xml,Image,Button,我在活动中找到了几种解决方案,但我更愿意坚持使用xml进行布局和图形 我想在按钮上设置一个带有圆角的图像,而不是普通的默认角 XML示例: <ImageView android:id="@+id/featuredimage" android:layout_width="0dp" android:layout_height="fill_parent" android
<ImageView
android:id="@+id/featuredimage"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:background="@drawable/rounded"
android:src="@drawable/budlight_sample"
android:scaleType="centerCrop" />
ROUNDED.XML
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" >
</corners>
</shape>
在xml文件中绘制一个形状,然后调用它
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#Anycolor" android:endColor="#Anycolor"
android:angle="90"/>
<corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
</shape>
另一个选项是使用draw9patch。但是,您必须为此创建一个自定义图像按钮。使用上述代码更容易。使用shape drawable可绘制圆角,在应用程序的drawable文件夹中定义rounded.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape=["rectangle" | "oval" | "line" | "ring"] >
<corners
android:radius="integer"
android:topLeftRadius="integer"
android:topRightRadius="integer"
android:bottomLeftRadius="integer"
android:bottomRightRadius="integer" />
</shape>
我使用“android:background=@drawable/img”将我的图像放在背景中,既然我不能使用重复的android:background描述,我现在该怎么做呢?不,你不能使用多个android:background,请使用图像视图集android:src=“你的图像”、android:background=“你的形状”和android:clickable=“true”图像仍然是一个矩形,还有其他想法吗?首先将形状更改为矩形,暂时删除您的src图像,并检查您是否获得圆角。我通过向xml添加android:cropToPadding=“true”来计算它为我裁剪圆角内的图片。谢谢你的帮助!我用“android:background=@drawable/img”将我的图像放在背景中,既然我不能使用重复的android:background描述,那么我现在该怎么做呢?如果你有一个图像想用作按钮,并且你想在按钮上添加圆角,我相信你可以做两件事
android:background=@drawable/img |@drawable/rounded
不确定这是否有效。我认为最简单的方法是在photoshop中为图像添加圆角,并使用draw9patch或创建多个尺寸的图像以实现兼容性。
android:background="@drawable/shape"