如何在android中以编程方式更改android:drawableRight
这是我的活动代码:如何在android中以编程方式更改android:drawableRight,android,Android,这是我的活动代码: public class Serchresult extends Activity implements OnClickListener { ImageView imageView1; String Status; String Reason; TextView status; TextView reason; @Override protected void onCreate(Bundle savedInstance
public class Serchresult extends Activity implements OnClickListener {
ImageView imageView1;
String Status;
String Reason;
TextView status;
TextView reason;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_serchresult);
Intent intent = getIntent();
Status = intent.getExtras().getString("Status");
Reason = intent.getExtras().getString("Reason");
imageView1 = (ImageView) findViewById(R.id.searchstatus_imgBack);
imageView1.setOnClickListener(this);
status = (TextView) findViewById(R.id.status);
reason = (TextView) findViewById(R.id.reason);
reason.setText(Reason);
if (reason.equals("ACCEPTED")) {
// reason.setCompoundDrawables(null,null,getResources().getDrawable(R.drawable.accept_icon),null);
Drawable accepted = getResources().getDrawable(
R.drawable.accept_icon);
accepted.setBounds(0, 0, accepted.getIntrinsicWidth(),
accepted.getIntrinsicHeight());
reason.setCompoundDrawables(null, null, accepted, null);
} else if (reason.equals("REJECTED")) {
// reason.setCompoundDrawables(null,null,getResources().getDrawable(R.drawable.reject_icon),null);
Drawable rejected = getResources().getDrawable(
R.drawable.reject_icon);
rejected.setBounds(0, 0, rejected.getIntrinsicWidth(),
rejected.getIntrinsicHeight());
reason.setCompoundDrawables(null, null, rejected, null);
}
else {
reason.setCompoundDrawables(null, null, null, null);
}
status.setText(Status.replace("null", ""));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.searchstatus_imgBack) {
finish();
}
}
我想以编程方式更改android:drawableRight意味着当接受来自服务器的条件时,图像图标应该不同如果拒绝,那么图像图标应该不同我无法做到这一点请哪里做错了帮助我用户下面的代码
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_weight="0.65"
android:background="#ffffff"
android:gravity="center"
android:padding="10dp" >
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:drawableRight="@drawable/ic_launcher"
android:gravity="center"
android:text="Accepted"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0060a4"
android:textSize="20dp" />
</LinearLayout>
其中SetCompoundDrawableSwithinInstructBounds的值为
Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
yourView.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);
使用SetCompoundDrawableSwithinInstincBounds:,int,int,intI尝试了它不工作Dudereason.equals接受,reason是一个文本视图,您的代码没有意义then@user2794306我总是用它。它工作得很好。为什么在我调试代码时不在我的代码中工作,并且在这种情况下运行,但图像不会出现请检查我的Textview XML请查看我的XML您会理解的原因。equals接受,原因是Textview,您的代码没有意义,然后检查此代码我已应用,但静止图像不是changing@user2794306您不能在参数为StringExact的TextView上调用.equals!do,reason.gettext.toString.equalsXXXXX
setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)