Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在按钮中间添加一个图像?_Android_Xml_Layout - Fatal编程技术网

Android 在按钮中间添加一个图像?

Android 在按钮中间添加一个图像?,android,xml,layout,Android,Xml,Layout,所以我在布局中创建了一个按钮,但我希望它是一个圆。 所以我创建了一个.xml可绘制文件,如下所示- <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <solid android:color="#673ab7"/> <stroke

所以我在布局中创建了一个按钮,但我希望它是一个圆。 所以我创建了一个.xml可绘制文件,如下所示-

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <solid android:color="#673ab7"/>
    <stroke android:width="2sp" android:color="#fff" />
</shape>
但现在我希望在圆圈按钮内有一个图像。我怎样才能做到这一点?我可以简单地在可绘制的xml中添加一些东西吗

我尝试过寻找答案,但我对应该搜索的内容感到困惑。

尝试以下内容:

<ImageButton
        android:id="@+id/imgBtn"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@drawable/circle_button"
        android:src="@drawable/image_to_insert"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

尝试以下操作:

<ImageButton
        android:id="@+id/imgBtn"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="@drawable/circle_button"
        android:src="@drawable/image_to_insert"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter" />

你为什么不试试相对论呢。只需将具有圆形图像的相对布局作为背景。唯一的问题是你需要圆形的图像

比如说,

    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/circle">  //circular shaped image circle.png
    </RelativeLayout>
//圆形图像circle.png

最后,您只需将侦听器应用于布局。这会解决你的问题。

你为什么不试试相对论呢。只需将具有圆形图像的相对布局作为背景。唯一的问题是你需要圆形的图像

比如说,

    <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/circle">  //circular shaped image circle.png
    </RelativeLayout>
//圆形图像circle.png

最后,您只需将侦听器应用于布局。这将解决您的问题。

最简单的方法是创建一个RelativeLayout,而不是按钮,将您的图像放在版面中,并将其属性设置为true


这将是在相对论的中间。并将
android:background=“@drawable/circle_按钮”
设置为RelativeLayout。现在,使用它id获取RelativeLayout,并向RelativeLayout添加一个单击侦听器。最简单的方法是创建一个RelativeLayout而不是按钮,将图像放入版面中,并将其属性设置为true


这将是在相对论的中间。并将
android:background=“@drawable/circle_按钮”
设置为RelativeLayout。现在使用it id获取RelativeLayout,并向RelativeLayout添加一个单击侦听器

谢谢,我必须将circle_按钮更改为src,然后我将背景设置为我的图像。看起来很好。很高兴,你找到了解决方案。此解决方案有缺陷。请阅读此。谢谢,我必须将circle_按钮更改为src,然后我将背景设置为我的图像。似乎工作正常。很高兴,您找到了解决方案。此解决方案存在缺陷。请阅读此内容。查看我的答案并向我提供反馈。查看我的答案并向我提供反馈。如果正确答案已经存在,则不需要添加相同的答案。你可以投票赞成那个答案。这就是stackoverflow的工作原理。首先,取一个名称user3811114。给出的答案不如我的正确。在stackoverflow中,您可以提交多个答案。这样你就可以找到多种方法来完成工作。如果正确的答案已经存在,那么你就不需要添加相同的答案。你可以投票赞成那个答案。这就是stackoverflow的工作原理。首先,取一个名称user3811114。给出的答案不如我的正确。在stackoverflow中,您可以提交多个答案。这样你就可以找到多种方法来完成这项工作。请给我解释一下如何在中间拍摄图像。再次阅读问题并编辑你的答案。向我解释如何在中间拍摄图像。再次阅读问题并编辑您的答案。