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控件时,首先需要在 ...
随机推荐
- Javascript DOM 02 在<ul>中创建、删除 <li>
创建DOM元素 createElement(标签名) 创建一个节点 appendChild(节点) 追加一个节点 例子:为ul插入li 插入元素 insertBefore(节点, 原有节点) 在 ...
- Linux下动态库使用
1. 静态库和动态库的基本概念 静态库,是在可执行程序连接时就已经加入到执行码中,在物理上成为执行程序的一部分:使用静态库编译的程序运行时无需该库文件支持,哪里都可以用, 但是生成的可执行文件较大.动 ...
- Storyboard中使用UIscrollView添加约束的开发总结
第一次在项目中用storyboard做界面,一般的界面直接添加约束非常爽快 然后有个界面有scrollview,添加了约束还总是出错 刚开始使用了 wCompact,hRegular,滑动出现问题,有 ...
- Mac编程的官方文档(类似MSDN)
https://developer.apple.com/library/mac/documentation/FileManagement/Conceptual/FileSystemProgrammin ...
- WCF技术剖析之三十:一个很有用的WCF调用编程技巧[下篇]
原文:WCF技术剖析之三十:一个很有用的WCF调用编程技巧[下篇] 在<上篇>中,我通过使用Delegate的方式解决了服务调用过程中的异常处理以及对服务代理的关闭.对于<WCF技术 ...
- 黑龙江省第七届大学生程序设计竞赛-Heap
描述 A heap is a full binary tree; for each node, its key is greater than its two sub-node’s key. Two ...
- leetcode 编辑距离
class Solution { public: int minDistance(string word1, string word2) { // Start typing your C/C++ so ...
- C# - 使用皮肤
运行效果: 项目目录结构: 主窗体代码: using System; using System.Collections.Generic; using System.ComponentModel; us ...
- Matlab,Visio等生成的图片的字体嵌入问题解决方法
确保所有字体嵌入,是生成高质量学术论文的必要条件.但是在Windows下,总会遇到Matlab或Visio生成字体没有嵌入的问题,当然这个问题的解决办法有很多(例如,对于Visio可以这样做:直接拷贝 ...
- MinGW 介绍
SDL新手教程(一):3.MinGW 下的安装与设置 作者:龙飞 3.1:MinGW 是什么? MinGW 提供了一套简单方便的Windows下的基于GCC 程序开发环境.MinGW 收集了一系列免费 ...
