android setCompoundDrawables 无法显示图片,TextView 设置图片。

1 篇文章 0 订阅
1 篇文章 0 订阅

Android动态生成简单UI。

1.获取drawable图片:

Drawable img = getResources().getDrawable(R.drawable.demo);
        img.setBounds(0,0,500,500);

2.创建TextView 视图

        TextView textView = new TextView(this);  
        textView.setText("hello world");
        textView.setCompoundDrawablePadding(18);
        textView.setPadding(3,3,3,3);
        textView.setBackgroundColor(Color.rgb(255, 0, 0));
        textView.setCompoundDrawables(img ,null,null,null);

        LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        Linear_FU_2.addView(textView,textLayoutParams);

1)创建一个textview视图 

TextView textView = new TextView(this);

2)设置textView 文字

textView.setText("hello world");

3)设置图片与字体的间距

textView.setCompoundDrawablePadding(18);

4)这里设置textview视图的一个内边距

textView.setPadding(3,3,3,3);  //四个值分别代表是(左,上,右,下)

5)设置textview视图颜色

textView.setBackgroundColor(Color.rgb(255, 0, 0));  //对应rgb电脑的三色数

6)在textview视图加入图片

textView.setCompoundDrawables(img ,null,null,null);  //里面的值对应是 图片在left,top,right,bottom 显示。

7)这里相当于设一个LInearLayout 并且设置一个layout_width ,layout_height 的一个值。

LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

android setCompoundDrawables 无法显示图片简单的解决方法

img.setBounds(0,0,500,500);  //这里是设置图片的高宽   500,500

setCompoundDrawables 无法显示可能是 setBounds() 设置的值过高或者太低  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值