Android中有时需动态设置控件四周的drawble图片,这个时候就需要调用 setCompoundDrawables(left, top, right, bottom),四个参数类型都是drawable
 
Button继承TextView,所以可以采用相同的设置方法
 
方法一.XML方式
 

<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"/>
 
方法二.JAVA代码
 
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的更多相关文章

  1. android Textview动态设置大小

    import android.app.Activity; //import com.travelzen.tdx.BaseActivity; //import com.travelzen.tdx.uti ...

  2. Android textView 动态设置代码字号大小,支持单位选项 dp,sp or px

    setTextSize(TypedValue.COMPLEX_UNIT_PX,22); //22像素 setTextSize(TypedValue.COMPLEX_UNIT_SP,22); //22S ...

  3. 【原创】如何在Android中为TextView动态设置drawableLeft等

    如何在Android中为TextView动态设置drawableLeft等   两种方式:   方式1:手动设置固有边界 Drawable drawable = getResources().getD ...

  4. 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中<meta-data>变量的值

    转载请说明来源: http://www.cnblogs.com/lizhilin2016/p/7390079.html 最近lz 在开始做一个新的Demo, 在项目中集成了bugly用于收集项目中的崩 ...

  5. 使用 gradle 在编译时动态设置 Android resValue / BuildConfig / Manifes中&lt;meta-data&gt;变量的值

    转载请标明出处:http://blog.csdn.net/xx326664162/article/details/49247815 文章出自:薛瑄的博客 你也能够查看我的其它同类文章.也会让你有一定的 ...

  6. Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度

     Android StatusBarUtil:设置Android系统下方虚拟键键盘透明度 Android StatusBarUtil是github上的一个开源项目,主页:https://githu ...

  7. Android中动态设置GridView的列数、列宽和行高

    在使用GridView时我们知道,列数是可以通过设计时的属性来设置的,列的宽度则是根据列数和GridView的宽度计算出来的.但是有些时候我们想实现列数是动态改变的效果,即列的宽度保持某个值,列的数量 ...

  8. android 怎么动态设置button 的style

    网上找了很多,还是没有直接的解决办法,button没有setstyle这个方法.因此我的解决办法如下: 直接动态设置各个属性 Button themeBtn = new Button(this); t ...

  9. android 如何动态设置View的margin和padding

    感谢大佬:https://blog.csdn.net/a107494639/article/details/7341077 1.动态设置padding,拿ImageView为例: ImageView ...

随机推荐

  1. Ubuntu安装MediaInfo

    Ubuntu版 打包下载:艺搜下载 适用于Ubuntu 12.10(i386) 安装libzen0_v0.4.29 _i386.xUbuntu_12.10.deb 安装libmediainfo0_v0 ...

  2. 【这特么是个坑。。。】iOS 10.3下解决Charles抓包ssl证书信任问题

    针对近期iOS 10.3以上的系统charles抓https信任问题 前言 最近iPhone系统更新到ios 10.3后,在公司里用Charles抓包竟然出现了一些问题,https的请求都会失败,提示 ...

  3. selenium测试(Java)--截图(十九)

    package com.test.screenshot; import java.io.File; import java.io.IOException; import org.apache.comm ...

  4. C struct的内存对齐

    说明:如果你看到了这篇,请略过. struct是复合类型. 其中的成员在内存中的分布都是对齐的. 这个对齐的意思是,struct的sizeof运算结果必定是其最大类型长度的整数倍. --注意,如果st ...

  5. linux -- Ubuntu图形界面终端实现注销、关机、重启

    linux命令行下的开关机分别为: 注销:logout 关机:shutdown -h now (参数不同,会有不同的关机模式) 重启:shutdown -r now 或 reboot 这些命令都是在l ...

  6. c#后台常用知识

    生成如:2015-10-25T12:12:12格式的时间 DateTime.Now.ToString("s") 非asp.net mvc环境下对url编码 (HttpUtility ...

  7. xshell-常用指令汇总 linux 常用指令

    suse linux 常用命令  (1)命令ls——列出文件  ls -la 给出当前目录下所有文件的一个长列表,包括以句点开头的“隐藏”文件  ls a* 列出当前目录下以字母a开头的所有文件  l ...

  8. informatica中的workflow连接远程数据库

    如果是远程oracle这样写 名称随便起,方便自己记住,后面用户名密码你都知道,再加上数据库的地址:端口/SID就可以了. 如10.33.2.208:1521/torcl

  9. 对ChemDraw Professional 16.0你了解多少

    ChemDraw Professional 16.0组件为科学家提供了最新的科学智能应用程序组件,绘制化学结构图和分析生物路径图.  ChemDraw Professional 16.0 ChemDr ...

  10. MathType中常见的两种符号的运用

    想要让公式编辑得快速又高效,MathType数学公式编辑器这个神助攻是少不了的.MathType是一款专用的数学公式编辑器,用它来编辑公式非常方便实用,并且排版也非常简单.下面介绍两种常见符号的应用. ...