Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标


注:(图中每一个条目和图标都是由代码动态生成)


代码动态布局,并需要为每一个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon" 


父xml文件:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/background" >  
  6.     <!-- 子布局由代码动态生成 -->  
  7.     <LinearLayout  
  8.         android:id="@+id/layout_CONTENT"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:padding="@dimen/content_padding" >  
  12.   
  13.         <LinearLayout  
  14.             android:id="@+id/activity_service_select"  
  15.             android:layout_width="match_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:layout_margin="@dimen/table_margin"  
  18.             android:background="@color/table_background"  
  19.             android:orientation="vertical"  
  20.             android:padding="@dimen/table_padding" >  
  21.         </LinearLayout>  
  22.     </LinearLayout>  
  23.   
  24. </ScrollView>  

子xml文件:

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="match_parent"  
  3.     android:layout_height="@dimen/row_height"  
  4.     android:layout_marginBottom="@dimen/row_margin"  
  5.     android:background="@drawable/row_selector"  
  6.     android:paddingLeft="@dimen/row_padding_left"  
  7.     android:paddingRight="@dimen/row_padding_right" >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/tv_select_item"  
  11.         style="@style/text_18"  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="@dimen/row_height"  
  14.         android:layout_marginBottom="@dimen/row_margin"  
  15.         android:background="@drawable/row_selector"  
  16.         android:gravity="center_vertical"  
  17.         android:textColor="@drawable/row_text_selector" />  
  18.   
  19.     <ImageView  
  20.         android:id="@+id/iv_icon"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="match_parent"  
  23.         android:layout_alignParentRight="true"  
  24.         android:duplicateParentState="true"  
  25.         android:gravity="center_vertical"  
  26.         android:src="@drawable/go" />  
  27.   
  28. </RelativeLayout>  

代码中引用:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. private ViewGroup mLayout;  
  2.     private int img[] = {R.drawable.zikao1,R.drawable.zikao2,R.drawable.zikao3,R.drawable.zikao4};  
  3.     /* (non-Javadoc) 
  4.      * @see app.ui.TitleActivity#onCreate(android.os.Bundle) 
  5.      */  
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setUpViews();  
  10.     }  
  11.   
  12.     private void setUpViews()  
  13.     {  
  14.         setContentView(R.layout.activity_service_select);  
  15.         setTitle(R.string.text_select);  
  16.         showBackwardView(R.string.button_backward, true);  
  17.         mLayout = (ViewGroup)findViewById(R.id.activity_service_select);  
  18.         final String [] mSelfSelect = getResources().getStringArray(R.array.text_self_select);  
  19.         // 需要布局的行数  
  20.         final int rowCount = mSelfSelect.length;  
  21.         for (int i = 0; i < rowCount; i++) {  
  22.             final LinearLayout linearLayout = new LinearLayout(this);  
  23.             View.inflate(this, R.layout.service_examvaluable_item, linearLayout);  
  24.             final View view = linearLayout.getChildAt(0);  
  25.             view.setTag(i+1);  
  26.             view.setOnClickListener(this);  
  27.   
  28.             Drawable drawable= getResources().getDrawable(img[i]);  
  29.             drawable.setBounds(00, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
  30.   
  31.             final TextView mTextView = (TextView) linearLayout.findViewById(R.id.tv_select_item);  
  32.             mTextView.setCompoundDrawables(drawable,null,null,null);//设置TextView的drawableleft  
  33.             mTextView.setCompoundDrawablePadding(10);//设置图片和text之间的间距  
  34.             mTextView.setText(mSelfSelect[i]);  
  35.             // 添加到屏幕布局  
  36.             LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);  
  37.             mLayout.addView(linearLayout, layoutParams);  
  38.         }  
  39.     }  



在程序中直接取出子xml中TextView中的id,并动态设置改变了 DrawableLeft。

解决方案:

[java]  view plain copy
  1. public void  setCompoundDrawables  (Drawable left, Drawable top, Drawable right, Drawable bottom);  


类似调用方法如下:

1.在XML中使用

[java]  view plain copy
  1. android:drawableLeft="@drawable/icon"  


2.代码中动态变化

[java]  view plain copy
  1. Drawable drawable= getResources().getDrawable(R.drawable.drawable);  
  2. drawable.setBounds(00, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
  3. myTextview.setCompoundDrawables(drawable,null,null,null);  

参考另一个函数:

[java]  view plain copy
  1. public void setCompoundDrawablesWithIntrinsicBounds (Drawable left,  
  2. Drawable top, Drawable right, Drawable bottom)  
  • 11
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值