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