gpt4 book ai didi

android - 更改按钮的背景图像

转载 作者:行者123 更新时间:2023-11-29 02:07:47 25 4
gpt4 key购买 nike

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

这是我的 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,您可以通过以下方式访问这些按钮:按钮 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>

关于android - 更改按钮的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9065009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com