zoukankan      html  css  js  c++  java
  • setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别

      前言:今天看源码看到的setCompoundDrawablesWithIntrinsicBounds方法当时初步理解就是在view的不同方向设置drawable图像,上网上查了查,就发现还有一个

    setCompoundDrawables的方法。手工设置文本与图片相对位置时,比如edittext中的设置左边图像或者右边图像一样,这里就来说说他们的区别

      他们都是TextView的方法,如果不设置就可以在相应的参数位置上设置为null

      1.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

       <1>api

    Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables must already have had setBounds(Rect) called.
    
    意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。但是Drawable必须已经setBounds(Rect)。意思是你要添加的资源必须已经设置过初始位置、宽和高等信息。

      这下就明白了,这个方法要先给Drawable设置setBounds(x,y,width,height);

        x:组件在容器X轴上的起点

        y:组件在容器Y轴上的起点

        组件的长度

        height:组件的高度。

      <2>用法

    1 mBtn = (Button) findViewById(R.id.id_btn);
    2         Drawable icon = this.getResources().getDrawable(R.drawable.ic_launcher);
    3         // 必须设置
    4         icon.setBounds(1, 1, 100, 100);
    5         mBtn.setCompoundDrawables(null, null, icon, null);

       效果:

         

      2.setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)

      <1>Api

    Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
    
    意思大概就是:可以在上、下、左、右设置图标,如果不想在某个地方显示,则设置为null。图标的宽高将会设置为固有宽高,既自动通过getIntrinsicWidth和getIntrinsicHeight获取。

      <2>用法

    1     mBtn = (Button) findViewById(R.id.id_btn);
    2         Drawable icon = this.getResources().getDrawable(R.drawable.ic_launcher);
    3         mBtn.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null);

       效果

  • 相关阅读:
    selenium 安装
    创建项目/执行
    mysql远程访问
    如何通过批处理文件直接运行python代码
    python中通过字典实现函数指针
    装饰器
    正则表达式学习笔记
    Jupyter使用
    【数学】一张通往数学世界的地图-阅读笔记
    【算法导论】二叉搜索树的删除除操作
  • 原文地址:https://www.cnblogs.com/dukc/p/5137194.html
Copyright © 2011-2022 走看看