getLayoutParams().width/getMeasuredWidth()/getWidth()

1.getLayoutParams().width

getLayoutParams().width可以在onMesure()方法中获取 。
若在xml文件中定义了该view的具体宽度,如android:layout_width="500dp",view.getLayoutParams().width就为500;
若定义为android:layout_width="match_parent",则view.getLayoutParams().width就为-1;
若定义为android:layout_width="wrap_content",则view.getLayoutParams().width就为-2;

public static final int FILL_PARENT = -1;
public static final int MATCH_PARENT = -1;
public static final int WRAP_CONTENT = -2;

getLayoutParams().width返回的是该view向父view请求的最大宽度,应该是接近实际宽度,可能不是view实际绘画的宽度。

ps:

1). getLayoutParams()方法的调用必须要有父控件,否则返回的就是null对象。该方法返回的是该控件的LayoutParams对象。

    /**
     * Get the LayoutParams associated with this view. All views should have
     * layout parameters. These supply parameters to the <i>parent</i> of this
     * view specifying how it should be arranged. There are many subclasses of
     * ViewGroup.LayoutParams, and these correspond to the different subclasses
     * of ViewGroup that are responsible for arranging their children.
     *
     * This method may return null if this View is not attached to a parent
     * ViewGroup or {@link #setLayoutParams(android.view.Vie
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值