简体   繁体   English

Android中按钮的背景图片

[英]Background image for a button in android

I am trying to add a background image for a button and it is working, but not as I expected. 我正在尝试为按钮添加背景图像,并且该图像正在工作,但是没有达到我的预期。

1st picture (without background) 第一张照片(无背景)

2nd picture (with background) 第二张图片(有背景)

I would like the background to cover only the button. 我希望背景仅覆盖按钮。 In other words, the image should have the same form as the 2 buttons from above (I'm talking about the round corners and a bit smaller size). 换句话说,图像应具有与上方2个按钮相同的形式(我说的是圆角和较小的尺寸)。

Here is the xml of the layout: 这是布局的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>

its easy to do that 这很容易做到

you should use ImageButton and remove those Buttons for example 您应该使用ImageButton并删除这些按钮,例如

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

also if you want to remove the background of that button you can use : 如果您想删除该按钮的背景,也可以使用:

android:background="@null"

Replace your layout with these layout...It can be helpfil for you 用这些布局替换您的布局...可以为您提供帮助

    <?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>

The simplest solution is: You need to edit your image background (make it a little smaller and have corner) - it can be done easily by using Photoshop 最简单的解决方案是:您需要编辑图像背景(使其更小并带有角)-使用Photoshop可以轻松完成
Than use 9-patch tool for make 9-patch image (9-patch images can automatically resize to accommodate the contents of the view and the size of the screen) 比使用9补丁工具制作9补丁图像(9补丁图像可以自动调整大小以适应视图内容和屏幕尺寸)

The "without background" button is actually displaying it default background, so setting new background will override that corner-rounded-gray thing ... “无背景”按钮实际上是在显示默认背景,因此设置新背景将覆盖该圆角为灰色的东西 ...

Check this question . 检查这个问题

I recommend using ImageButton with android:src instead. 我建议将ImageButtonandroid:src使用。

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

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