简体   繁体   English

想要在Button上添加图标,drawableTop无法正常工作

[英]Want to add icon on Button, drawableTop is not working Android

I want to add call icon on Button but drawableTop is not working. 我想在Button上添加呼叫图标,但drawableTop无法正常工作。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">
        <Button
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:drawableTop="@android:drawable/ic_menu_call"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

在此处输入图片说明

Replace your Button with AppCompatButton Button替换为AppCompatButton

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:drawableTop="@android:drawable/ic_menu_call"/>
</LinearLayout>

添加定位到你LinearLayout ,决定是否要设置android:layout_weight而设置android:layout_width="wrap_content"android:layout_height="match_parent" ,并设置文本的按钮。

You can also define the icon in drawableStart like in this way: 您也可以像这样在drawableStart中定义图标:

                    <Button
                        android:id="@+id/imageView3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/black_overlay"
                        android:drawableStart="@drawable/ic_menu_call"
                        android:padding="15dp"
                        android:text="Click here"
                        android:textStyle="bold" />

The padding, text style and background color is only for the decoration. 填充,文本样式和背景颜色仅用于装饰。

when using androidx with the material ui library instead of the support library 当使用带有材质ui库而不是支持库的androidx时

implementation 'com.google.android.material:material:1.0.0'

you will need to use 您将需要使用

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop="@android:drawable/ic_menu_camera"
    android:text="Camera" />

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM