【问题标题】:Background image for a button in androidandroid中按钮的背景图像
【发布时间】:2015-12-25 10:03:45
【问题描述】:

我正在尝试为按钮添加背景图片,它正在工作,但与我预期的不同。

1st picture (without background)

2nd picture (with background)

我希望背景只覆盖按钮。换句话说,图像应该与上面的 2 个按钮具有相同的形式(我说的是圆角和小一点的尺寸)。

这是布局的xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
android:orientation="vertical">

<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button
        android:onClick="playerStats"
        android:text="@string/plStats"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button

        android:text="@string/comp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

<LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button
        android:background="@drawable/calendar"
        android:text="@string/calendar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
</LinearLayout>

【问题讨论】:

  • 你试过用scaletype

标签: android button layout background-image android-linearlayout


【解决方案1】:

很容易做到这一点

例如,您应该使用 ImageButton 并删除这些按钮

<ImageButton
    android:src="@drawable/calendar"
    android:text="@string/calendar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

如果你想删除那个按钮的背景,你可以使用:

android:background="@null"

【讨论】:

    【解决方案2】:

    用这些布局替换您的布局...它可以为您提供帮助

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity"
    android:orientation="vertical">
    
    <LinearLayout android:layout_weight="1" android:layout_height="match_parent" android:layout_width="match_parent">
        <Button
            android:onClick="playerStats"
            android:text="@string/plStats"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </LinearLayout>
      <View    ////I HAVE ADDED VIEW WHICH IS TRASNPARENT TO SEPRATE THE BUTTONS
        android:layout_width="fill_parent"
        android:layout_height="10dp"
    
        android:background="@android:color/transparent" >
    </View>
    
    
    <LinearLayout android:layout_weight="1" android:layout_height="match_parent" android:layout_width="match_parent">
        <Button
    
            android:text="@string/comp"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </LinearLayout>
    
      <View ////I HAVE ADDED VIEW WHICH IS TRASNPARENT TO SEPRATE THE BUTTONS
        android:layout_width="fill_parent"
        android:layout_height="10dp"
    
        android:background="@android:color/transparent" >
    </View>
    
    
    <LinearLayout android:layout_weight="1" android:layout_height="match_parent" android:layout_width="match_parent">
        <Button
            android:background="@drawable/calendar"
            android:text="@string/calendar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />
    </LinearLayout>
    
    <View  ////I HAVE ADDED VIEW WHICH IS TRASNPARENT TO SEPRATE THE BUTTONS
        android:layout_width="fill_parent"
        android:layout_height="10dp"
    
        android:background="@android:color/transparent" >
    </View>
    

    【讨论】:

    • 我已经尝试过您的代码,但这并不是我所需要的;它只是在按钮之间留了一些空间
    • @LerulLer ...你还想要什么,兄弟??
    • 我希望第三个按钮的图像(来自第二张图片)与第一张图片中的第三个按钮具有相同的大小/形式(如果仔细观察,第二张图片中的图像是比第一张图片中的按钮大一点)
    • @LerulLer ..您必须使用dimen方法...您可以指定android的各个dimen文件夹的按钮高度
    • @LerulLer ...我已经编辑了答案..请检查它...代替 LINERLAYOUT 中的 FILL_PARENT ..我替换为 MATCH_APRENT ....它将为您工作。 .你可以试试...如果有问题请告诉我
    【解决方案3】:

    最简单的解决方案是:您需要编辑图像背景(使其更小并有角) - 使用 Photoshop 可以轻松完成
    比使用9-patch工具制作9-patch图像(9-patch图像可以自动调整大小以适应视图内容和屏幕大小)

    【讨论】:

      【解决方案4】:

      “无背景”按钮实际上显示的是默认背景,因此设置新背景将覆盖那个圆角灰色的东西...

      检查this question

      我建议改用 ImageButtonandroid:src

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-07
        • 1970-01-01
        • 2012-12-16
        • 2016-04-13
        • 2011-02-19
        • 2012-09-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多