简体   繁体   English

更改按钮的背景图像

[英]Change the background image of a button

I am using two buttons, one for next news and one for previous news. 我正在使用两个按钮,一个用于下一则新闻,一个用于先前新闻。 In my activity when I am on the first news my button should set the image with the grey image and when I move forward my previous news button should change from the grey image to the colored one. 在活动中,当我收到第一个新闻时,我的按钮应将图像设置为灰色图像,而当我向前移动时,我先前的新闻按钮应从灰色图像变为彩色图像。 Similarly when I reach at the last news my next button should change to grey image. 同样,当我到达最新消息时,我的下一个按钮应更改为灰色图像。

This is my xml file. 这是我的xml文件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFFFF" 
    >

    <TextView
        android:id="@+id/tv_submitDate"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:text="Medium Text"
        android:textColor="#000000" 
        android:padding="5dp"/>

    <TextView
        android:id="@+id/tv_headline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_submitDate"
        android:text="Medium Text"
        android:textColor="#000000"
        android:textStyle="bold" 
        android:padding="5dp"/>

    <View
        android:id="@+id/viewSeperator"
        android:layout_width="fill_parent"
        android:layout_height="2dip"
        android:layout_below="@+id/tv_headline"
        android:background="#FF909090" />

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_relative_layout"
        android:layout_below="@+id/viewSeperator" android:padding="10dp">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TextView
                android:id="@+id/tv_detailNews"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Medium Text"
                android:textColor="#000000" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/btn_relative_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#DCDCDC"
        android:padding="10sp" >


        <Button
            android:id="@+id/btn_prevNews"
            android:layout_width="120sp"
            android:layout_height="40sp"
            android:layout_weight="0.5"
            android:layout_marginRight="5sp"
            android:background="@drawable/arrow_left"
            android:clickable="true" />


        <Button
            android:id="@+id/btn_nextNews"
            android:layout_width="120sp"
            android:layout_height="40sp"
            android:layout_marginLeft="5sp"
            android:layout_weight="0.5"
            android:background="@drawable/arrow_right"
            android:clickable="true" />
        </LinearLayout>


</RelativeLayout>

This is where I am trying to change the image of my button: 这是我尝试更改按钮图像的地方:

public void onClick(View v) {

        if (v == btnPrevNews && newsId > 0) {
            if (newsId > 0) {
                newsId--;
                putDetailNewsinView(newsId);
            } else if (newsId == 0) {
                btnPrevNews
                        .setBackgroundResource(R.drawable.disable_arrow_left);
                btnPrevNews.setEnabled(false);
            }
            // btnPrevNews.setVisibility(View.INVISIBLE);

        } else if (v == btnNextNews && newsId < newsListDetail.size() - 1) {
            if (newsId < newsListDetail.size() - 1) {
                newsId++;
                putDetailNewsinView(newsId);
                if(newsId == 0)
                    btnNextNews.setBackgroundDrawable(getResources().getDrawable(
                            R.drawable.disable_arrow_right));
            } else if (newsId == newsListDetail.size()) {
                // btnNextNews.setVisibility(View.INVISIBLE);
                btnNextNews.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.disable_arrow_right));
                // btnNextNews.setBackgroundResource(R.drawable.disable_arrow_right);
                btnNextNews.setEnabled(false);
            }
        }

    }

Please help me out in this. 请帮助我。

Thanks in advance. 提前致谢。

I assume you have Methods which will be executed if the Button is pressed. 我假设您有如果按下按钮将要执行的方法。 Just use these and add Logic to change the Background of these Buttons to grey: Give both Buttons an ID, you can access these Buttons with: Button mybutton = (Button) findViewById(R.id.yourid); 只需使用这些按钮并添加Logic即可将这些按钮的背景更改为灰色:为两个按钮提供ID,您可以使用以下命令访问这些按钮: Button mybutton = (Button) findViewById(R.id.yourid);

Now you can do all kinds of things with your button, like changing the background. 现在,您可以使用按钮执行各种操作,例如更改背景。

edit: The Problem could lay in the Pictures, but if i were you, i would set some breakpoints and debug step-by-step. 编辑:问题可能出在图片上,但是如果我是你,我会设置一些断点并逐步调试。

I hacked some code of mine to test this functionality, it works fine for me. 我破解了我的一些代码来测试此功能,对我来说很好用。 Here is it: 就这个:

    package my.namespac;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class SomeTestProjectActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button)findViewById(R.id.btn_prevNews);

        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Button b = (Button)v;
                v.setBackgroundDrawable(getResources().getDrawable(R.drawable.koala));

            }
        });
    }
}

And the Layout-Xml: 和Layout-Xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


            <Button
            android:id="@+id/btn_prevNews"
            android:layout_width="120sp"
            android:layout_height="40sp"
            android:layout_weight="0.5"
            android:layout_marginRight="5sp"
            android:background="@drawable/ic_launcher"
            android:clickable="true" />
</LinearLayout>

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

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