Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 可提取资源ID_Android - Fatal编程技术网

Android 可提取资源ID

Android 可提取资源ID,android,Android,如何动态地将drawable的位置从android:drawableLeft更改为android:drawableRight/left/bottom? 我知道,我应该使用setcompoundDrawableSwithinInstructBounds方法,但首先我需要知道R.drawable.*并在setcompoundDrawableSwithinInstructBounds方法中使用它 private void changeLabelPosition(View view){ int

如何动态地将drawable的位置从android:drawableLeft更改为android:drawableRight/left/bottom? 我知道,我应该使用setcompoundDrawableSwithinInstructBounds方法,但首先我需要知道R.drawable.*并在setcompoundDrawableSwithinInstructBounds方法中使用它

private void changeLabelPosition(View view){
    int drawableID = 
    if(getGravity = Gravity.LEFT){
        view.setCompoundDrawablesWithIntrinsicBounds(drawableID, 0, 0, 0);
    }else if(getGravity = Gravity.TOP){
        view.setCompoundDrawablesWithIntrinsicBounds(0, drawableID, 0 , 0);
    }else if(getGravity = Gravity.RIGHT){
        view.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableID, 0);
    }else if(getGravity = Gravity.BOTTOM){
        view.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, drawableID);
    }    
}
所以,我需要视图的可绘制资源的id

这种方式是不合适的,因为有很多按钮,而且它不是使用硬代码的好方法:

drawableID = 0;
switch (view.getId()){
case R.id.button_add:
    drawableID = R.drawable.button_add;
case R.id.button_remove:
    drawableID = R.drawable.button_remove;
...
...
...
}
if(getGravity = Gravity.LEFT){
    view.setCompoundDrawablesWithIntrinsicBounds(drawableID, 0, 0, 0);
}
...
...
...
enter code here
编辑

switch (mMainPanelGravity)
 { 
case Gravity.TOP:
 for (View v : arrayOfViews)
 { 
Drawable[] drawables = ((TextView) v) .getCompoundDrawables();
 if (drawables.length != 0) { 
Drawable dr = drawables[0]; 
((TextView) v).setCompoundDrawablesWithIntrinsicBounds( null, dr, null, null); 
}

setCompoundDrawablesWithIntrinsicBounds(左、上、右、下)
也将接受
Drawable

您可以获得视图的
可绘制
,并可以直接设置它

Drawable drawable=yourview.getBackground();
然后

view.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null)
编辑:

Drawable[] drawables = view.getCompoundDrawables()

if(drawables.length!=0)
{
Drawable drawable=drawable[0];
}

@user2038145在这个意义上不起作用,首先要弄清楚您试图实现什么drawable的位置正在从xml文件设置为android:drawableRight。文本在正确的位置,但可绘制的不可见。@user2038145我真的不理解您的问题。它将直接获取可绘制文件而不是id,仅此而已..它与您的代码有何不同。?请发布更新的代码开关(mMainPanelGravity){case Gravity.TOP:for(View v:arrayOfView){drawable[]drawables=((TextView)v).getCompoundDrawables();if(drawables.length!=0){Drawable dr=drawables[0];((TextView)v).setCompoundDrawablesWithIntrinsicBounds(null,dr,null,null);}