Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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_Android Layout - Fatal编程技术网

Android 如何将图像作为按钮的背景,同时仍应用可绘制文件

Android 如何将图像作为按钮的背景,同时仍应用可绘制文件,android,xml,android-layout,Android,Xml,Android Layout,我试图有4个按钮,每个按钮都有一个可绘制的文件应用于它们,改变它们的形状和颜色,但我还需要在每个按钮内有一个图像。我尝试使用“android:drawableStart=“@drawable/icon\u\u\u white”方法,但问题是图像没有停留在按钮的中心。 这是我的xml文件,包含所有4个按钮: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.

我试图有4个按钮,每个按钮都有一个可绘制的文件应用于它们,改变它们的形状和颜色,但我还需要在每个按钮内有一个图像。我尝试使用“android:drawableStart=“@drawable/icon\u\u\u white”方法,但问题是图像没有停留在按钮的中心。 这是我的xml文件,包含所有4个按钮:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/appBarMain"
        layout="@layout/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize" />

    <androidx.drawerlayout.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/textView6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="20dp"
                android:layout_marginEnd="20dp"
                android:text="TextView"
                android:textAlignment="textStart"
                android:textAllCaps="true"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:paddingLeft="20dp"
                android:paddingRight="20dp">

                <Button
                    android:id="@+id/button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/confirmed_boxes"
                    android:drawableStart="@drawable/icon_lodging_white" />

                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/white_confirm_boxes" />

                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="3dp"
                    android:layout_weight="1"
                    android:background="@drawable/white_confirm_boxes"/>

                <Button
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@drawable/white_confirm_boxes" />
            </LinearLayout>

            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/option_tab"
                android:layout_marginStart="20dp"
                android:layout_marginEnd="20dp" />

        </LinearLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:fitsSystemWindows="true" />
    </androidx.drawerlayout.widget.DrawerLayout>

</RelativeLayout>

您可以在一个按钮中放置两个可抽绳:

  • 背景:金色形状用作按钮的背景
  • src:背景上方的中心图像,轮廓形状

你能展示一下你想要实现的目标吗?泰,我刚从“按钮”改为“图像按钮”,然后我用“安卓:src”设置图像,“安卓:背景”设置按钮的形状和颜色