【问题标题】:Want to add icon on Button, drawableTop is not working Android想要在 Button 上添加图标,drawableTop 在 Android 上不起作用
【发布时间】:2019-01-18 11:37:20
【问题描述】:

我想在 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>

【问题讨论】:

  • 并使用相同的代码我可以看到drawableTop
  • 实际上我正在使用 android:drawableLeft ,这也不起作用。
  • 有没有你drawable的tint颜色和背景颜色一样的情况?
  • 您是否有机会使用新材料库而不是设计支持库?

标签: android android-layout android-drawable android-button


【解决方案1】:

将您的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>

【讨论】:

  • 您是否使用新材料库代替支持库?
  • 我正在使用 com.android.support:design:28.0.0
【解决方案2】:

当使用带有材质ui库而不是支持库的androidx时

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

你需要使用

<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" />

【讨论】:

  • 你知道我们需要使用drawableTop而不是icon的原因吗?
【解决方案3】:

为您的LinearLayout 添加方向,在设置android:layout_width="wrap_content"android:layout_height="match_parent" 时决定是否要设置android:layout_weight,并为您的按钮设置文本。

【讨论】:

    【解决方案4】:

    你也可以像这样在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" />
    
    

    内边距、文字样式和背景颜色仅用于装饰。

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 2016-04-21
      • 1970-01-01
      • 2021-02-16
      • 2014-09-22
      相关资源
      最近更新 更多