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控件时,首先需要在 ...
随机推荐
- 【译】在Asp.Net中操作PDF – iTextSharp - 使用表格
原文 [译]在Asp.Net中操作PDF – iTextSharp - 使用表格 使用Asp.Net生成PDF最常用的元素应该是表格,表格可以帮助比如订单或者发票类型的文档更加格式化和美观.本篇文章并 ...
- MUI跳转页面传值
1.打开新的页面.通过 mui.openWindow 打开页面extras参数传递参数 mui.openWindow({ id: "yingshou-" + newid, url: ...
- HTML——左右側边栏布局
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 2783: [JLOI2012]树( dfs + BST )
直接DFS, 然后用set维护一下就好了.... O(nlogn) ------------------------------------------------------------------ ...
- BZOJ 1342: [Baltic2007]Sound静音问题( 单调队列 )
一开始写了个RMQ然后就T了... 好吧正解是单调队列, 维护两个单调队列... ----------------------------------------------------------- ...
- js和循环依赖
kmdjs和循环依赖 循环依赖是非常必要的,有的程序写着写着就循环依赖了,可以提取出一个对象来共同依赖解决循环依赖,但是有时会破坏程序的逻辑自封闭和高内聚.所以没解决好循环依赖的模块化库.框架.编译器 ...
- ubuntu 安装LaTex
使用IDE来使用tex,如TexMaker. 1.到http://www.tug.org/texlive/acquire-netinstall.html 下载texlive. 2.linux下命令行, ...
- 基于visual Studio2013解决面试题之1204大数组查找
题目
- KMP算法 KMP模式匹配 一(串)
A - KMP模式匹配 一(串) Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:131072KB ...
- Determine whether an integer is a palindrome. Do this without extra space.
看到这个题目的时候,首先不认识 Determine这个单词.英文不好没办法,查了下是确认的意思,然后不懂 palindrome这个单词, 查了下是回文的意思. 问题是 回文是个什么东西,官方解释: A ...
