如何在Android中以编程方式更改android:drawableRight

时间:2014-11-17 11:03:00

标签: android

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

    }

这是我的活动代码:

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

我想改变android:drawableRight编程意味着当接受条件来自服务器时它的图像图标应该不同,如果拒绝然后图像图标应该不同我无法做到这一点请在哪里做错了帮助我

1 个答案:

答案 0 :(得分:0)

用户下面的代码,

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
yourView.setCompoundDrawablesWithIntrinsicBounds(imgResource, 0, 0, 0);

其中setCompoundDrawablesWithIntrinsicBounds的值为

setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)