Android-onMeasure使用解决Scrollview嵌套listview冲突

来源:互联网 发布:g92多头螺纹编程实例 编辑:程序博客网 时间:2024/06/06 13:58

    (一)冲突情况如图

                                             


       解决冲突后:


                                                                               

  (二)解决方法:自定义一个MyListView,使用onMeasure方法测量(设置)MyListview的宽和高

 

  (三) 难点:

           1、 onMeasure( int widthMeasureSpec,int heightMeasureSpec) 方法

   自学中查资料看到很多博客中大神都是直接使用,

 int a=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
         1.1 MeasureSpec: A MeasureSpec encapsulates the layout requirements passed from parent to child( 官方解    释)

      1.2 makeMeasureSpec(size,mode)

          参数size:是设置大小的,可以自己写入数值(比如200),

          参数mode:是模式,系统提供了MeasureSpec.EXACTLY、MeasureSpec.AT_MOST、MeasureSpec.UNSPECIFIED三种模式


       因为int  a最大位数为32位,前两位用于表示模式的类型,后30位才是真正的用于表示size,所以使用Integer.MAX_VALUE>>2,表示size是最大的
                                                                                             

  代码

       activity_main.xml

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical"><com.example.xx.scroll_and_listview.MyListView    android:id="@+id/listview"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:entries="@array/app_list"></com.example.xx.scroll_and_listview.MyListView>   <LinearLayout       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:orientation="vertical"       android:gravity="center">       <Button           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="点我"/>       <Button           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="点我"/>       <Button           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="点我"/>       <Button           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="点我"/>       <Button           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="点我"/>       <Button           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="点我"/>       <Button           android:layout_width="wrap_content"           android:layout_height="wrap_content"           android:text="点我"/>   </LinearLayout>    </LinearLayout></ScrollView>


MyListView.java

public class MyListView extends ListView {    public MyListView(Context context) {        super(context);    }    public MyListView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         int a=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);         int b=MeasureSpec.makeMeasureSpec(200,MeasureSpec.AT_MOST);                  super.onMeasure(b, a);   }}


  

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 羞羞影院免费影片 强犴视频网站 女生被舔照片视频 床上运动奶视屏 00后小萝莉 喷液视频完整 2019看黄不付费 十部挑战人伦底线的电影 标准七十二种插法不遮不演 床上99种捅法 36种插法不遮不挡视频 儿子曰亲妈 妈装唾视频 爷爷插妈视频 老婆非让我睡她妈视频 真实儿曰亲妈视频观看 单身儿子单身妈 儿子再用点力妈要飞了中文 在上海陪读初三的儿子 加油儿子妈要流了txt 啊儿子深点妈要来了txt 儿子上来妈教你的故事 儿子在快一点妈要飞了视频 单身妈喜欢找儿子 中国家庭毋HH伦s线播放普通话 中国家庭真实毋HH伦s线播放 儿子你快点搞你爸要回来吗 儿子快点你爸回来了嗯 儿子你搞快点你爸要回来啦文字 快点你爸马上就回来了视频 儿子你搞快点你爸要回来啦电影 儿子快拿出来你爸快回来了视频 儿子你要高就快点你爸回来了电影 儿子你要就快点你爸要回来了小说 儿子 快点妈要来了漫画 你快点啊你爸快回来了视频 儿子快点你爸快回来了在哪能看 儿子搞快点你爸要回来了短文 儿子你快停下你爸要醒了视频 儿子用点力妈 儿子深一点用点力阅读 你要搞就快点你爸快回来了视频 试衣间深一点用点力视频