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


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

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


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


父xml文件:

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <ScrollViewxmlns: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. <LinearLayout
  13. android:id="@+id/activity_service_select"
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_margin="@dimen/table_margin"
  17. android:background="@color/table_background"
  18. android:orientation="vertical"
  19. android:padding="@dimen/table_padding">
  20. </LinearLayout>
  21. </LinearLayout>
  22. </ScrollView>

子xml文件:

[html] view plain copy print ?
  1. <RelativeLayoutxmlns: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. <TextView
  9. android:id="@+id/tv_select_item"
  10. style="@style/text_18"
  11. android:layout_width="match_parent"
  12. android:layout_height="@dimen/row_height"
  13. android:layout_marginBottom="@dimen/row_margin"
  14. android:background="@drawable/row_selector"
  15. android:gravity="center_vertical"
  16. android:textColor="@drawable/row_text_selector"/>
  17. <ImageView
  18. android:id="@+id/iv_icon"
  19. android:layout_width="wrap_content"
  20. android:layout_height="match_parent"
  21. android:layout_alignParentRight="true"
  22. android:duplicateParentState="true"
  23. android:gravity="center_vertical"
  24. android:src="@drawable/go"/>
  25. </RelativeLayout>

代码中引用:

[java] view plain copy print ?
  1. privateViewGroupmLayout;
  2. privateintimg[]={R.drawable.zikao1,R.drawable.zikao2,R.drawable.zikao3,R.drawable.zikao4};
  3. /*(non-Javadoc)
  4. *@seeapp.ui.TitleActivity#onCreate(android.os.Bundle)
  5. */
  6. @Override
  7. protectedvoidonCreate(BundlesavedInstanceState){
  8. super.onCreate(savedInstanceState);
  9. setUpViews();
  10. }
  11. privatevoidsetUpViews()
  12. {
  13. setContentView(R.layout.activity_service_select);
  14. setTitle(R.string.text_select);
  15. showBackwardView(R.string.button_backward,true);
  16. mLayout=(ViewGroup)findViewById(R.id.activity_service_select);
  17. finalString[]mSelfSelect=getResources().getStringArray(R.array.text_self_select);
  18. //需要布局的行数
  19. finalintrowCount=mSelfSelect.length;
  20. for(inti=0;i<rowCount;i++){
  21. finalLinearLayoutlinearLayout=newLinearLayout(this);
  22. View.inflate(this,R.layout.service_examvaluable_item,linearLayout);
  23. finalViewview=linearLayout.getChildAt(0);
  24. view.setTag(i+1);
  25. view.setOnClickListener(this);
  26. Drawabledrawable=getResources().getDrawable(img[i]);
  27. drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
  28. finalTextViewmTextView=(TextView)linearLayout.findViewById(R.id.tv_select_item);
  29. mTextView.setCompoundDrawables(drawable,null,null,null);//设置TextView的drawableleft
  30. mTextView.setCompoundDrawablePadding(10);//设置图片和text之间的间距
  31. mTextView.setText(mSelfSelect[i]);
  32. //添加到屏幕布局
  33. LayoutParamslayoutParams=newLinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
  34. mLayout.addView(linearLayout,layoutParams);
  35. }
  36. }



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

解决方案:

[java] view plain copy
  1. publicvoidsetCompoundDrawables(Drawableleft,Drawabletop,Drawableright,Drawablebottom);


类似调用方法如下:

1.在XML中使用

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


2.代码中动态变化

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

参考另一个函数:

[java] view plain copy
  1. publicvoidsetCompoundDrawablesWithIntrinsicBounds(Drawableleft,
  2. Drawabletop,Drawableright,Drawablebottom)
原文链接: http://blog.csdn.net/gao_chun/article/details/38284507

更多相关文章

  1. Android Studio使用Lint进行代码检查
  2. 初学Android做计时器和代码
  3. Android UI 开发入门—线性布局
  4. android 源代码构建和运行cts
  5. 实现TextView的垂直滚动以及通过代码设置TextView滚动无法显示滚
  6. Android Studio使用XML样式在JAVA代码中的使用(使用java代码调节x
  7. Android下使用dlopen函数动态调用.so链接库 [转]
  8. Android中apk动态加载技术研究(1)基础知识研修

随机推荐

  1. Android换肤Demo
  2. 有了这个技巧,即便领导盯着你,你都能给他玩
  3. Android(安卓)RecyclerView控件
  4. 个人经验 - Android的RelativeLayout布局
  5. Android(安卓)提高学习计划
  6. android:layout_above="@+id/XXX"与andro
  7. Android开发和安全系列工具
  8. Android(安卓)左侧抽屉 +点击事件
  9. Android(安卓)在子线程中更新UI
  10. 在 Ubuntu 下配置 Android(安卓)开发环境