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

注:(图中每个条目和图标都是由代码动态生成)
代码动态布局,并须要为每个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon"
父xml文件:
- <?
xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/background" >
- <!-- 子布局由代码动态生成 -->
- <LinearLayout
- android:id="@+id/layout_CONTENT"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="@dimen/content_padding" >
- <LinearLayout
- android:id="@+id/activity_service_select"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_margin="@dimen/table_margin"
- android:background="@color/table_background"
- android:orientation="vertical"
- android:padding="@dimen/table_padding" >
- </LinearLayout>
- </LinearLayout>
- </ScrollView>
子xml文件:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="@dimen/row_height"
- android:layout_marginBottom="@dimen/row_margin"
- android:background="@drawable/row_selector"
- android:paddingLeft="@dimen/row_padding_left"
- android:paddingRight="@dimen/row_padding_right" >
- <TextView
- android:id="@+id/tv_select_item"
- style="@style/text_18"
- android:layout_width="match_parent"
- android:layout_height="@dimen/row_height"
- android:layout_marginBottom="@dimen/row_margin"
- android:background="@drawable/row_selector"
- android:gravity="center_vertical"
- android:textColor="@drawable/row_text_selector" />
- <ImageView
- android:id="@+id/iv_icon"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_alignParentRight="true"
- android:duplicateParentState="true"
- android:gravity="center_vertical"
- android:src="@drawable/go" />
- </RelativeLayout>
代码中引用:
- 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)
- */
- @Override
- protected 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 < rowCount; i++) {
- final LinearLayout linearLayout = new LinearLayout(this);
- View.inflate(this, R.layout.service_examvaluable_item, linearLayout);
- final View view = linearLayout.getChildAt(0);
- view.setTag(i+1);
- view.setOnClickListener(this);
- Drawable drawable= getResources().getDrawable(img[i]);
- drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
- final TextView mTextView = (TextView) linearLayout.findViewById(R.id.tv_select_item);
- mTextView.setCompoundDrawables(drawable,null,null,null);//设置TextView的drawableleft
- mTextView.setCompoundDrawablePadding(10);//设置图片和text之间的间距
- mTextView.setText(mSelfSelect[i]);
- // 加入到屏幕布局
- LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
- mLayout.addView(linearLayout, layoutParams);
- }
- }
在程序中直接取出子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)
Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性加入图标的更多相关文章
- 动态为TextView控件设置drawableLeft图标,并设置间距
效果图: 重要属性: textView.setCompoundDrawablePadding(4);//设置图片和text之间的间距 textView.setPadding(-5, 0, 0, 0); ...
- 两个TextView控件居中显示
通过一个线性布局将两个TextView控件包装在一起,设置LinearLayout的layout_centerInParent属性为true即可.代码如下 <LinearLayout andro ...
- 注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式
注意Android里TextView控件的一个小坑,用android:theme来设置样式时动态载入的layout会丢失该样式 这个坑,必须要注意呀, 比如在用ListView的时候,如果在List_ ...
- android中的TextView控件
我以前是搞ssh开发的,现在在搞android开发,所以简单学习了一下,对于自己所了解的做一个记录,也算是一个笔记吧,如果有什么不对的,希望大家给予一定的指导. 一.TextView的基本使用 Te ...
- android课程第一节(TextView控件使用)
TextView控件使用 一.TextView基本使用(创建方式) 1.在程序中创建TextView对象 如下代码: @Override protected void onCreate(Bundle ...
- 在ASP.NET中动态加载内容(用户控件和模板)
在ASP.NET中动态加载内容(用户控件和模板) 要点: 1. 使用Page.ParseControl 2. 使用base.LoadControl 第一部分:加载模板 下 面是一个模板“<tab ...
- WPF中动态加载XAML中的控件
原文:WPF中动态加载XAML中的控件 using System; using System.Collections.Generic; using System.Linq; using System. ...
- android软件开发之TextView控件常用属性
TextView控件 text属性,设置显示的文本 textColor:设置文本颜色 textSize:设置文本字体大小 autoLink:设置文本为电话,URL连接等的时候是否显示为可点击的链接 c ...
- Android学习笔记50:使用WebView控件浏览网页
在Android中,可以使用Webview控件来浏览网页.通过使用该控件,我们可以自制一个简单的浏览器,运行效果如图1所示. 图1 运行效果 1.WebView 在使用WebView控件时,首先需要在 ...
随机推荐
- Ganglia 权威指南-安装Ganglia过程
转自于:http://blog.csdn.net/xxd851116/article/details/21527055 http://www.dataguru.cn/article-3816-1.ht ...
- MySQL - 主从复制及读写分离原理
1.MySQL主从复制与读写分离原理 首先,我们看一个图: 影响MySQL-A数据库的操作,在数据库执行后,都会写入本地的日志系统A中. 假设,实时的将变化了的日志系统中的数据库事件操作,在MYSQL ...
- SSH-Struts(一)——基本原理
简单介绍 Struts框架是MVC的一个实现,它非常好的结合了JSP.Servlet.JavaBean.Taglib等技术.它为MVC的各层提供了良好的支持,就像房地产商盖房子时先盖的大楼框架. 仅仅 ...
- WPF DataGrid 增加"更新"模板列,根据行Row的选择而显示"更新"按钮
SelectionMode="Single" <DataGridTemplateColumn Header=""> <DataGridTemp ...
- 在VC++中使用Tab Control控件
系统环境:Windows 7软件环境:Visual Studio 2008 SP1本次目的:在模态或非模态对话框中使用Tab Control控件,及引申在单/多文档中使用 查阅MSDN文档,对于创建T ...
- SharePoint 2013 "通知我"简单的功能
简单的功能 "通知我"内部列表或文档库中的主要项目.加入/删除/修改等操作,用户的E- mail通知设定功能:设置列表或文档库通知的能力,有可能设置通知为一个单一的项目.这是Sha ...
- POJ 2250(最长公共子序列 变形)
Description In a few months the European Currency Union will become a reality. However, to join the ...
- js关键字与保留字的坑。
在写一个算法,迷宫出口的算法,作为一个有追求的前端,首先在解决算法的问题之前要把迷宫的图做的漂漂亮亮的才对得住自己的审美,所以我花了一个钟的时间去写这个地图. 不过这次我们说的并不是迷宫的解法,也不是 ...
- MFC类中获得其它类指针
当用VC++的Application Wizard生成除了CDialog Basiced以外的应用程序时,将自动产生视图类.文档类.主帧窗口类.应用程序类等等.一般来说,程序的核心数据及操作在文档类中 ...
- Javascript做模糊查询
<html> <head> <title>Javascript模糊查找</title> </head> <body> <l ...
