一个按钮的背景图象在机器人

时间:2015-12-25 10:03:45

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

我正在尝试为按钮添加背景图像,但它正在运行,但并不像我预期的那样。

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>

4 个答案:

答案 0 :(得分:3)

很容易做到

你应该使用ImageButton并删除那些按钮,例如

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

如果您想删除该按钮的背景,也可以使用:

android:background="@null"

答案 1 :(得分:1)

用这些布局替换你的布局...它可以帮助你

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

&#34;没有背景&#34;按钮实际上是显示默认背景,所以设置新背景会覆盖那个圆角灰色的东西 ......

检查this question

我建议使用ImageButton代替android:src