更改按钮的背景图像
我使用两个按钮,一个用于下一条新闻,一个用于上一条新闻。在我的活动中,当我看到第一个新闻时,我的按钮应该将图像设置为灰色图像,当我向前移动时,我的上一个新闻按钮应该从灰色图像更改为彩色图像。同样,当我到达最后一条新闻时,我的下一个按钮应该更改为灰色图像。
这是我的 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);
}
}
}
请帮助我。
提前致谢。
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您有按下按钮时将执行的方法。只需使用这些并添加逻辑即可将这些按钮的背景更改为灰色:
为这两个按钮提供一个 ID,您可以通过以下方式访问这些按钮:
Button mybutton = (Button) findViewById(R.id.yourid);
现在您可以使用按钮执行各种操作,例如更改背景。
编辑:
问题可能出在图片中,但如果我是你,我会设置一些断点并逐步调试。
我破解了我的一些代码来测试此功能,它对我来说效果很好。这是它:
和 Layout-Xml:
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);
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:
And the Layout-Xml: