如何将背景图像添加到形状中?我在下面尝试过但没有成功的代码:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
//here is where i need to set the image
<solid android:color="@drawable/button_image"/>
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
答案 0 :(得分:181)
使用以下layerlist
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" android:padding="10dp">
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
</item>
<item android:drawable="@drawable/image_name_here" />
</layer-list>
答案 1 :(得分:174)
我将以下内容用于具有圆形背景的可绘制图像。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@color/colorAccent"/>
</shape>
</item>
<item
android:drawable="@drawable/ic_select"
android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp"/>
</layer-list>
这是它的样子
希望能帮助别人。
答案 2 :(得分:19)
这是一个圆圈形状,里面有图标:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ok_icon"/>
<item>
<shape
android:shape="oval">
<solid android:color="@color/transparent"/>
<stroke android:width="2dp" android:color="@color/button_grey"/>
</shape>
</item>
</layer-list>
答案 3 :(得分:4)
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@color/orange"/>
</shape>
</item>
<item
android:top="2dp"
android:bottom="2dp"
android:left="2dp"
android:right="2dp">
<shape android:shape="oval">
<solid android:color="@color/white"/>
</shape>
</item>
<item
android:drawable="@drawable/messages" //here messages is my image name, please give here your image name.
android:bottom="15dp"
android:left="15dp"
android:right="15dp"
android:top="15dp"/>
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/merchant_circle" /> // here merchant_circle will be your first .xml file name
答案 4 :(得分:4)
这是一个角落图片
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/img_main_blue"
android:top="5dp"
android:bottom="5dp"
android:right="5dp"
android:left="5dp"/>
<item>
<shape android:shape="rectangle" android:padding="10dp">
<corners android:radius="10dp"/>
<stroke android:width="5dp"
android:color="@color/white"/>
</shape>
</item>
</layer-list>
答案 5 :(得分:1)
这是获取图像自定义形状的另一种最简单方法(图像视图)。这可能对某人有帮助。它只是一行代码。
首先,您需要添加依赖项:
dependencies {
compile 'com.mafstech.libs:mafs-image-shape:1.0.4'
}
然后只需编写一行代码:
Shaper.shape(context,
R.drawable.your_original_image_which_will_be_displayed,
R.drawable.shaped_image_your_original_image_will_get_this_images_shape,
imageView,
height,
weight);