获取视图的当前背景ID

Sna*_*gon 4 android drawable android-layout

我有一个布局,其背景在drawable中定义,我想在某些条件下将其更改为另一个.如何获取当前背景的标识符以了解它是什么?

Ama*_*Yus 8

这可能是一个非常古老的问题,但万一人们还在寻找它:

if(yourView.getBackground().getConstantState().equals(
    getResources().getDrawable(R.drawable.yourdrawable).getConstantState()){

  //do something if this is the correct drawable
}
Run Code Online (Sandbox Code Playgroud)


Tal*_*lha 3

你好,你可以试试这个例子,

btnNew =(Button)findViewById(R.id.newButton); 

 // compare newlist
if(newButton.getBackground()!=(findViewById(R.id.imgAdd)).getBackground()) 
{ 
   btnNew.setBackgroundResource(R.drawable.imgDelete); 
}
Run Code Online (Sandbox Code Playgroud)