代码动态布局,并需要为每一个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon"
父xml文件:
子xml文件:
代码中引用:
private ViewGroup mLayout;private int img[] = {R.drawable.zikao1,R.drawable.zikao2,R.drawable.zikao3,R.drawable.zikao4};/* (non-Javadoc)* @see app.ui.TitleActivity#onCreate(android.os.Bundle)*/@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setUpViews();}private void setUpViews(){setContentView(R.layout.activity_service_select);setTitle(R.string.text_select);showBackwardView(R.string.button_backward, true);mLayout = (ViewGroup)findViewById(R.id.activity_service_select);final String [] mSelfSelect = getResources().getStringArray(R.array.text_self_select);// 需要布局的行数final int rowCount = mSelfSelect.length;for (int i = 0; i
在程序中直接取出子xml中TextView中的id,并动态设置改变了 DrawableLeft。
解决方案:
- public void setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom);
类似调用方法如下:
1.在XML中使用
- android:drawableLeft="@drawable/icon"
2.代码中动态变化
- Drawable drawable= getResources().getDrawable(R.drawable.drawable);
- drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
- myTextview.setCompoundDrawables(drawable,null,null,null);
参考另一个函数:
- public void setCompoundDrawablesWithIntrinsicBounds (Drawable left,
- Drawable top, Drawable right, Drawable bottom)