【Android】TextView动态设置android:drawableLeft|Right|Top|Bottom,SetColor
<TextView
android:id="@+id/bookTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/checkmark"
android:drawableRight="@drawable/checkmark"
android:drawableTop="@drawable/checkmark"
android:gravity="center_vertical"
android:textStyle="bold"
android:textSize="20dip"
android:maxLines="1"
android:ellipsize="end"/>
Drawable img_on, img_off;
Resources res = getResources();
img_off = res.getDrawable(R.drawable.btn_strip_mark_off);
//调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示
img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight());
btn.setCompoundDrawables(img_off, null, null, null); //设置左图标
android 如何设置背景的透明度
半透明<Button android:background="#e0000000" ... />
透明<Button android:background="#00000000" ... />
颜色和不透明度 (alpha) 值以十六进制表示法表示。任何一种颜色的值范围都是 0 到 255(00 到 ff)。对于 alpha,00 表示完全透明,ff 表示完全不透明。表达式顺序是“aabbggrr”,其中aa=alpha(00 到 ff);bb=blue(00 到 ff);gg=green(00 到 ff);rr=red(00 到 ff)。例如,如果您希望对某叠加层应用不透明度为 50% 的蓝色,则应指定以下值:7fff0000
Java代码
View v = findViewById(R.id.content);//找到你要设透明背景的layout 的id
v.getBackground().setAlpha(100);//0~255透明度值 ,0为完全透明,255为不透明
package com.txlong; import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView; public class ListViewDemoActivity extends Activity {
// private ListView listView; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); TextView tv = new TextView(this);
tv.setText("Test set TextView's color.");
//方案一:通过ARGB值的方式
/**
* set the TextView color as the 0~255's ARGB,These component values
* should be [0..255], but there is no range check performed, so if they
* are out of range, the returned color is undefined
*/
// tv.setTextColor(Color.rgb(255, 255, 255));
/**
* set the TextView color as the #RRGGBB #AARRGGBB 'red', 'blue',
* 'green', 'black', 'white', 'gray', 'cyan', 'magenta', 'yellow',
* 'lightgray', 'darkgray'
*/
tv.setTextColor(Color.parseColor("#FFFFFF")); /** 原来不知道有上边的方法,就用这个笨笨方法了 */
// String StrColor = null;
// StrColor = "FFFFFFFF";
// int length = StrColor.length();
// if (length == 6) {
// tv.setTextColor(Color.rgb(
// Integer.valueOf(StrColor.substring(0, 2), 16),
// Integer.valueOf(StrColor.substring(2, 4), 16),
// Integer.valueOf(StrColor.substring(4, 6), 16)));
// } else if (length == 8) {
// tv.setTextColor(Color.argb(
// Integer.valueOf(StrColor.substring(0, 2), 16),
// Integer.valueOf(StrColor.substring(2, 4), 16),
// Integer.valueOf(StrColor.substring(4, 6), 16),
// Integer.valueOf(StrColor.substring(6, 8), 16)));
// } //方案二:通过资源引用
// tv.setTextColor(getResources().getColor(R.color.my_color)); //方案三:通过资源文件写在String.xml中
// Resources resource = (Resources) getBaseContext().getResources();
// ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.my_color);
// if (csl != null) {
// tv.setTextColor(csl);
// } //方案四:通过xml文件,如/res/text_color.xml
// XmlPullParser xrp = getResources().getXml(R.color.text_color);
// try {
// ColorStateList csl = ColorStateList.createFromXml(getResources(), xrp);
// tv.setTextColor(csl);
// } catch (Exception e) {
// } // listView = new ListView(this);
//
// Cursor cursor = getContentResolver().query(
// Uri.parse("content://contacts/people"), null, null, null, null);
//
// startManagingCursor(cursor);
//
// ListAdapter listAdapter = new SimpleCursorAdapter(this,
// android.R.layout.simple_expandable_list_item_2, cursor,
// new String[] { "name", "name" }, new int[] {
// android.R.id.text1, android.R.id.text2 });
//
// listView.setAdapter(listAdapter);
// setContentView(listView);
setContentView(tv);
}
}
String.xml文件为:
<?xml version="1.0" encoding="utf-8"?>
<resources> <string name="hello">Hello World, ListViewDemoActivity!</string>
<string name="app_name">ListViewDemo</string> <color name="my_color">#FFFFFF</color> </resources>
/res/color/text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#FF111111"/>
<!-- pressed -->
<item android:state_focused="true" android:color="#FF222222"/>
<!-- focused -->
<item android:state_selected="true" android:color="#FF333333"/>
<!-- selected -->
<item android:state_active="true" android:color="#FF444444"/>
<!-- active -->
<item android:state_checkable="true" android:color="#FF555555"/>
<!-- checkable -->
<item android:state_checked="true" android:color="#FF666666"/>
<!-- checked -->
<item android:state_enabled="true" android:color="#FF777777"/>
<!-- enabled -->
<item android:state_window_focused="true" android:color="#FF888888"/>
<!-- window_focused --> </selector>
【Android】TextView动态设置android:drawableLeft|Right|Top|Bottom,SetColor的更多相关文章
- android Textview动态设置大小
import android.app.Activity; //import com.travelzen.tdx.BaseActivity; //import com.travelzen.tdx.uti ...
- Android textView 动态设置代码字号大小,支持单位选项 dp,sp or px
setTextSize(TypedValue.COMPLEX_UNIT_PX,22); //22像素 setTextSize(TypedValue.COMPLEX_UNIT_SP,22); //22S ...
- 【原创】如何在Android中为TextView动态设置drawableLeft等
如何在Android中为TextView动态设置drawableLeft等 两种方式: 方式1:手动设置固有边界 Drawable drawable = getResources().getD ...
- 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中<meta-data>变量的值
转载请说明来源: http://www.cnblogs.com/lizhilin2016/p/7390079.html 最近lz 在开始做一个新的Demo, 在项目中集成了bugly用于收集项目中的崩 ...
- 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中<meta-data>变量的值
转载请标明出处:http://blog.csdn.net/xx326664162/article/details/49247815 文章出自:薛瑄的博客 你也能够查看我的其它同类文章.也会让你有一定的 ...
- Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度
Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度 Android StatusBarUtil是github上的一个开源项目,主页:https://githu ...
- Android中动态设置GridView的列数、列宽和行高
在使用GridView时我们知道,列数是可以通过设计时的属性来设置的,列的宽度则是根据列数和GridView的宽度计算出来的.但是有些时候我们想实现列数是动态改变的效果,即列的宽度保持某个值,列的数量 ...
- android 怎么动态设置button 的style
网上找了很多,还是没有直接的解决办法,button没有setstyle这个方法.因此我的解决办法如下: 直接动态设置各个属性 Button themeBtn = new Button(this); t ...
- android 如何动态设置View的margin和padding
感谢大佬:https://blog.csdn.net/a107494639/article/details/7341077 1.动态设置padding,拿ImageView为例: ImageView ...
随机推荐
- 历届蓝桥杯C/C++省赛试题
2012年第三届蓝桥杯C/C++程序设计本科B组省赛 2013年第四届蓝桥杯C/C++程序设计本科B组省赛 2014年第五届蓝桥杯C/C++程序设计本科B组省赛 2015年第六届蓝桥杯C/C++程序设 ...
- 树莓派 安装 刷Android Things 小结
一句话说,Android Things就是让开发者可以使用Android开发工具开发嵌入式设备. If you can build an app, you can build a device. 只要 ...
- html块状元素、内联元素
html块状元素.内联元素 原文在这 块级元素的分类 块级元素按照其应用于结构还是内容分为三种:结构化块状元素,终端块状元素,多目标块状元素. 一.结构化块状元素 这类元素用于构造文档的结构,一个好的 ...
- 《FPGA全程进阶----实战演练》第二章之系统搭建
1 系统方案 对于设计一款硬件平台,首先要确定整体框架,确定各个模块所需要的芯片以及电压分配情况.图2.6是笔者曾经设计的硬件平台系统. 图2.6系统框图 对于选定一个系统方案之后,接下来做的要先去查 ...
- 【转】C# Async/Await 异步编程中的最佳做法
Async/Await 异步编程中的最佳做法 Stephen Cleary 近日来,涌现了许多关于 Microsoft .NET Framework 4.5 中新增了对 async 和 await 支 ...
- 基于SSH框架、Oracle数据库、easyui的分页显示
要求:在easyui-datagrid中完成paginaton的分页功能. 1.easyui-datagrig的配置 <table id="dg" rownumbers=tr ...
- dirname(__FILE__) 介绍
简单地说: __FILE__ 返回当前 路径+文件名 dirname(__FILE__) 返回当前文件路径的 路径部分 (后面没有“\”号) dirname(di ...
- DoBox 下载
DoBox下载 一款简单十分好用的办公助手,用于记录您接下来需要做的事情.待办事项小工具 - DoBox DoBox下载 下载地址:http://www.wxzzz.com/?id=141 最新版本: ...
- 第三章 Spring.Net 环境准备和搭建
在前面一章我们介绍了依赖注入,控制反转的概念.接下来我们来真正动手搭建一下Spring.Net的环境,看一下Spring.Net 中的控制反转和依赖注入是什么样子. 3.1 Spring.Net 下 ...
- nuget修改配置文件
https://www.cnblogs.com/seejoy/p/8093837.html 然后将文件解压到需要打包的工程解决方案根目录下. 然后修改nuget文件夹下的 UploadNupkg.ex ...