更改按钮的背景图像

时间:2012-01-30 14:01:06

标签: android button background-image

我正在使用两个按钮,一个用于下一个新闻,一个用于以前的新闻。在我的活动中,当我在第一个新闻时,我的按钮应该用灰色图像设置图像,当我向前移动时,我的上一个新闻按钮应该从灰色图像变为彩色图像。同样,当我到达最后一条新闻时,我的下一个按钮应该变为灰色图像。

这是我的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>

这是我试图更改按钮图像的地方:

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);
            }
        }

    }

请帮帮我。

提前致谢。

1 个答案:

答案 0 :(得分:1)

我假设您有按下按钮时将执行的方法。只需使用这些并添加逻辑将这些按钮的背景更改为灰色: 给两个按钮一个ID,你可以访问这些按钮: Button mybutton = (Button) findViewById(R.id.yourid);

现在,您可以使用按钮执行各种操作,例如更改背景。

编辑:  问题可能存在于图片中,但如果我是你,我会设置一些断点并逐步调试。

我攻击了我的一些代码来测试这个功能,它对我来说很好。这是它:

    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));

            }
        });
    }
}

以及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>
相关问题