【问题标题】:how to set image as background button如何将图像设置为背景按钮
【发布时间】:2016-06-27 14:15:55
【问题描述】:

我是 Android 开发新手。我用一个简单的按钮创建了一个简单的项目。我想将图像设置为背景(见图)。我使用了 android:src="@drawable/id_image" 和 android:background="@drawable/id_image2" 但图像没有出现在按钮的整个表面上。任何帮助将不胜感激

【问题讨论】:

标签: android button background


【解决方案1】:

Android,您可以使用android.widget.ImageButton 显示一个普通的“按钮”,并带有自定义的背景图像。

<ImageButton
        android:id="@+id/top_bar_btn_right_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/ic_filter" />

【讨论】:

    【解决方案2】:

    您可以使用android:srcandroid:background 来添加两个图像。

    【讨论】:

    • 我使用了 android:src="@drawable/" 和 android:background="@drawable" 但图像没有出现在按钮的整个表面上
    • 你放的是android:src="@drawable还是android:src="@drawable/id_of_your_image
    【解决方案3】:

    当您添加按钮时,您只需转到属性菜单并选择背景/可绘制并选择您的图像

    【讨论】:

    • 我使用了 android:src="@drawable/" 和 android:background="@drawable" 但图像没有出现在按钮的整个表面上
    【解决方案4】:

    你可以试试这样的

    <RelativeLayout
        android:layout_width="250dp"
        android:layout_height="400dp"
        android:background="your_background_goes_here"
        android:foreground="?android:attr/selectableItemBackground"
        android:clickable="true"
        android:centerInParent="true">
    
        <ImageView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:src="your_icon_above_background"/>
    
       <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="text_for_your_image"/>
    
        </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多