简体   繁体   English

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

[英]how to have images as the background of a button while still having a drawable file applied to it

I'm trying to have 4 buttons and each of them has a drawable file applied to them which changes their shape and color, but i also need to have an image inside each button.我试图有 4 个按钮,每个按钮都有一个可绘制的文件应用于它们,改变它们的形状和颜色,但我还需要在每个按钮内都有一个图像。 I tried to use the "android:drawableStart="@drawable/icon_lodging_white"" method but the problem with this is that the image doesn't stay in the center of the button.我尝试使用 "android:drawableStart="@drawable/icon_lodging_white"" 方法,但问题是图像没有停留在按钮的中心。 Here's my xml file with all 4 buttons:这是我的 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>

You can put 2 drawables in a Button:您可以在 Button 中放置 2 个 drawable:

例子

  • background: use as the button's background, the golden shape背景:用作按钮的背景,金色形状
  • src: a centered image above the background, the profil shape src:背景上方居中的图像,轮廓形状

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 背景可绘制未应用于按钮 - Background Drawable is not getting applied to the Button 如何使按钮单击更加可见,并将XML可绘制资源文件设置为按钮的背景 - how do you make the button click more visible having a xml drawable resource file set as a background on the button 为什么我的可绘制按钮布局没有作为背景应用于按钮? - Why is my drawable button layout not being applied to the button as a background? 如何在仍然显示小部件的同时将我的 AppBar 放在 Flutter 中的单独文件中? - How can I have my AppBar in a separate file in Flutter while still having the Widgets show? 如何更改样式化的按钮背景颜色而不必为每个按钮创建可绘制对象? - How to change a styled button background colour without having to create a drawable for each of them? Android如何设置按钮具有透明背景但仍有轮廓 - Android how to set button to have transparent background but outline still there 如何用于适合背景图像的可绘制文件夹 - how drawable folder to use to fit background images 如何在Android中使用选择器绘制背景按钮? - how to get background button with selector drawable in android? 如何获取具有可绘制状态的按钮的背景色? - How to obtain Background color of a button with drawable state? 如何正确设置按钮的背景可绘制Android - How to Set the Background Drawable of a Button Properly Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM