当前位置:首页 > Android > 正文内容

Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标

jsc10年前 (2016-04-01)Android4781

1405913786_9066.jpg

注:(图中每一个条目和图标都是由代码动态生成)


代码动态布局,并需要为每一个条目设置图标,此时用到了 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)


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://www.jsc0.com/post/46.html

分享给朋友:

“Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性添加图标” 的相关文章

android json解析及简单例子

JSON的定义:       一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性。业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据交换。JSON采用兼容性很高的文本格式...

Android应用加入微信分享

Android应用加入微信分享

一、申请你的AppIDhttp://open.weixin.qq.com/  友情提示:推荐使用eclipse打包软件最后一步的MD5值去申请AppID二、官网下载libammsdk.jar包http://open.weixin.qq.com/download/?lang=zh_...

修改keystore密码别名等

修改keystore密码别名等

之前在测试Eclipse ADT的Custom debug keystore自定义调试证书的时候,发过一篇关于调试证书规格的博文:Eclipse ADT的Custom debug keystore所需证书规格,提到过自定义调试证书的密码和alias命名以及alias密码都是...

GridView中item高度自适应

item高度自适应public class MyAdapter extends BaseAdapter {         GridView mGv;&n...

制作一款Android APK管理器主要代码

Android APK管理器代码,主要就是两个列表,一个显示SD卡上面的APK文件的list,一个显示已经安装的app的list。1:获取SD卡上的APK安装文件后,要用代码读出APK里面的信息,如icon等,的主要代码如下:private void getUninatllApk...

获取Android手机中SD卡存储信息

SD卡作为手机的扩展存储设备,在手机中充当硬盘角色,可以让我们手机存放更多的数据以及多媒体等大体积文件。因此查看SD卡的内存就跟我们查看硬盘的剩余空间一样,是我们经常操作的一件事,那么在Android开发中,我们如何能获取SD卡的内存容量呢?首先,要获取SD卡上面的信息,必须先对SD卡有访问的权限,...