android 得到view的长度

1:android得到view的长度可以通过getMeasureWidth  getWidth   但是比如在一个view的构造方法或者初始化时候 用getWidth那么就是0,而getMeasureWidth只是粗略的估计了一下view的宽高,在调用getMeasureWidth 之前 调用 measure(0,0)   否则会得到0

2: getMeasuerWidth  不一定准确  比如 以下布局

            <LinearLayout
                
                    android:layout_width="fill_parent"

                    android:oritation="horizontal"
                    android:layout_height="wrap_content"
                <LinearLayout
                    android:id="@+id/ll_01"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >

                    <RadioButton
                        android:id="@+id/rd01"
                        android:layout_width="fill_parent"
                        android:layout_height="100dip"
                        android:background="@color/white"
                        android:button="@null"
                        android:checked="true"
                        android:drawableBottom="@null"
                        android:drawableLeft="@null"
                        android:drawableRight="@null"
                        android:drawableTop="@drawable/selector_color_white_light_blue"
                        android:gravity="center_horizontal|bottom"
                        android:paddingBottom="10dip"
                        android:text="全部" />
                </LinearLayout>


                <LinearLayout
                    android:id="@+id/ll_02"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" >

                    <RadioButton
                        android:id="@+id/rd02"
                        android:layout_width="fill_parent"
                        android:layout_height="100dip"
                        android:background="@color/white"
                        android:button="@null"
                        android:checked="true"
                        android:drawableBottom="@null"
                        android:drawableLeft="@null"
                        android:drawableRight="@null"
                        android:drawableTop="@drawable/selector_color_white_light_blue"
                        android:gravity="center_horizontal|bottom"
                        android:paddingBottom="10dip"
                        android:text="全部" />
                </LinearLayout>

   </LinearLayout>


这里面需要注意的是 当我们把一个view设置为fillparent 或者设置权重的时候 我们通过getMeasuerWidth得到的宽度是不准确的

raidiobutton的通过getMeasuerWidth得到的宽度 实际上是文字的宽度

所以一般我们在view的ondraw方法 和onLayout方法里面 通过getWidth得到宽度(并且在父view的onLayout方法里面也能通过子view的getWIdth得到宽度);



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值