更改按钮的背景图片
问题描述:
我正在使用两个按钮,一个用于下一个新闻,另一个用于之前的新闻。在我的活动中,当我在第一条新闻时,我的按钮应该用灰色图像设置图像,当我向前移动时,我的上一个新闻按钮应该从灰色图像变为彩色图像。同样,当我到达最新消息时,我的下一个按钮应该变成灰色图像。更改按钮的背景图片
这是我的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);
}
}
}
请帮我在这。
在此先感谢。
答
我假设你有方法,如果按下按钮,它将被执行。只要使用这些并添加逻辑来改变这些按钮的背景为灰色: 给两个按钮的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));
}
});
}
}
并且布局的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>
您好,欢迎!请告诉我们你到目前为止的情况。你有没有在代码或xml中定义按钮? – WarrenFaith 2012-01-30 14:03:11