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>

更多相关文章

  1. Android相关知识
  2. Android(安卓)绘图机制:canvas初解
  3. View类的XML属性、相关方法及说明
  4. Android(安卓)SDK下载和更新失败的解决方法!!!
  5. android布局动画实现方法
  6. ANDROID多点触控API接口的使用
  7. Android应用程序执行流程
  8. 浅谈Java中Collections.sort对List排序的两种方法
  9. Python list sort方法的具体使用

随机推荐

  1. Android的logcat输出
  2. View的状态属性简介
  3. Android之监听文本框输入的文字个数并实
  4. Android高斯模糊、高斯平滑(Gaussian Blur
  5. android:webView总结
  6. Android(安卓)Edittext输入限制一位小数2
  7. android studio导入quick cocos2dxV3.3的
  8. Android(安卓)EditText设置只能输入数字
  9. Android TextView 文字居中 .
  10. Android(安卓)Studio快捷键汇总