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



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



父xml文件:

  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. <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文件:

  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. <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>

代码中引用:

  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. private void setUpViews()
  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. final String [] mSelfSelect = getResources().getStringArray(R.array.text_self_select);
  18. // 须要布局的行数
  19. final int rowCount = mSelfSelect.length;
  20. for (int i = 0; i < rowCount; i++) {
  21. final LinearLayout linearLayout = new LinearLayout(this);
  22. View.inflate(this, R.layout.service_examvaluable_item, linearLayout);
  23. final View view = linearLayout.getChildAt(0);
  24. view.setTag(i+1);
  25. view.setOnClickListener(this);
  26. Drawable drawable= getResources().getDrawable(img[i]);
  27. drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
  28. final TextView mTextView = (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. LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
  34. mLayout.addView(linearLayout, layoutParams);
  35. }
  36. }

在程序中直接取出子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(0, 0, 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)

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性加入图标的更多相关文章

  1. 动态为TextView控件设置drawableLeft图标,并设置间距

    效果图: 重要属性: textView.setCompoundDrawablePadding(4);//设置图片和text之间的间距 textView.setPadding(-5, 0, 0, 0); ...

  2. 两个TextView控件居中显示

    通过一个线性布局将两个TextView控件包装在一起,设置LinearLayout的layout_centerInParent属性为true即可.代码如下 <LinearLayout andro ...

  3. 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式

    注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...

  4. android中的TextView控件

    我以前是搞ssh开发的,现在在搞android开发,所以简单学习了一下,对于自己所了解的做一个记录,也算是一个笔记吧,如果有什么不对的,希望大家给予一定的指导.  一.TextView的基本使用 Te ...

  5. android课程第一节(TextView控件使用)

    TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...

  6. 在ASP.NET中动态加载内容(用户控件和模板)

    在ASP.NET中动态加载内容(用户控件和模板) 要点: 1. 使用Page.ParseControl 2. 使用base.LoadControl 第一部分:加载模板 下 面是一个模板“<tab ...

  7. WPF中动态加载XAML中的控件

    原文:WPF中动态加载XAML中的控件 using System; using System.Collections.Generic; using System.Linq; using System. ...

  8. android软件开发之TextView控件常用属性

    TextView控件 text属性,设置显示的文本 textColor:设置文本颜色 textSize:设置文本字体大小 autoLink:设置文本为电话,URL连接等的时候是否显示为可点击的链接 c ...

  9. Android学习笔记50:使用WebView控件浏览网页

    在Android中,可以使用Webview控件来浏览网页.通过使用该控件,我们可以自制一个简单的浏览器,运行效果如图1所示. 图1 运行效果 1.WebView 在使用WebView控件时,首先需要在 ...

随机推荐

  1. 知识点1-3:MVC设计模式

    MVC代表模型-视图-控制器(Model-View-Controller),起源于20世纪70年代的Smalltalk开发社区,2003年起随着Ruby on Rails的出现才在Web上流行使用. ...

  2. Qt 状态机框架学习(没学会)

    Qt状态机框架是基于状态图XML(SCXML) 实现的.从Qt4.6开始,它已经是QtCore模块的一部分.尽管它本身是蛮复杂的一套东西,但经过和Qt的事件系统(event system).信号槽(s ...

  3. [置顶] 搭建apache+tomcat+memcached集群环境

    一.搭建apache server服务器 1.apache server图示:(加载图片要一张一张,所以可以到下面网站下载带图片的word) http://download.csdn.net/user ...

  4. httplib,urllib和urllib2

    一.httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib con ...

  5. [置顶] Hibernate从入门到精通(七)多对一单向关联映射

    上次的博文Hibernate从入门到精通(六)一对一双向关联映射中我们介绍了一下一对一双向关联映射,本次博文我们讲解一下多对一关联映射 多对一单向关联映射 多对一关联映射与一对一关联映射类似,只是在多 ...

  6. cocos2d-x的经历(含源码——)

    源于手游的火爆和大环境的影响,最近在学习cocos2d-x.其实以前一直有想搞andriod和ios的尝试,可惜本屌丝没钱买mac os x,只好先开始学习andriod,于是买了小米2s作为测试机, ...

  7. ios9怎么设置6位密码 ios9设置6位密码图文教程

    在原来的 iOS 系统中,只有四位数字密码与复杂密码之分,而在 iOS9 系统中却多出了一个六位数字密码,那ios9怎么设置6位密码,今天小编就为大家带来ios9设置6位密码图文教程   在原来的 i ...

  8. How to install vim on linux

    前几日了解到 vim 文本编辑器,据说很强大,使用起来效率很高,今天怀着很好奇的心理,学习了一下vim编辑器的下载安装方法,在此详细介绍一下安装vim编辑器的几种常用方法: 第一种方法就是在 Ubun ...

  9. SQL模板和模板实例化

    需求:需要得出一个数据源DataTable,我已知SQL和HttpRequest如何,通过SQL模板的方式去实例化匹配HttpRequest中的参数实例化为查询SQL,最后返回DataTable 1. ...

  10. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...