首先,ScrollView中只能放一个控件,一般都放LinearLayout,orientation属性值为vertical。在LinearLayout中放需要呈现的内容。ListView也在其中,ListView的高度设为适应自身内容(wrap_content)。

下面直接看例子吧:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.xxx.bokeyuan_16.MainActivity"> <ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/text_num"> </ListView> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button4" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button5" />
</LinearLayout>
</ScrollView> </LinearLayout>

  上面代码中的@array/text_num就是在trings中创建数据:

<resources>
<string-array name="text_num">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
</string-array>
</resources>

  得到的效果如下图:可以清楚的看到ListView里面的数据仅显示了text_num中的第一个数据。

下面通过自定义组件来解决此显示不全的问题:点击New,在其中创建一个MyListView类如下:

完成下列创建后,再在上面的layout中将ListView改成com.xxx.scrollview.MyListView

package com.xxx.scrollview;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView; /**
* Created by john on 2016/7/15.
*/
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) {
heightMeasureSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

如下图,现在再来运行就搞定了。

Scrollview中嵌套ListView(自定义组件解决)的更多相关文章

  1. Android 如何在ScrollView中嵌套ListView

    前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...

  2. Android实战技巧:如何在ScrollView中嵌套ListView

    前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...

  3. Android -- 在ScrollView中嵌套ListView

    在做一个工程,这个工程的布局可以相当的复杂,最外面是ScrollView,在ScrollView里面有两个Listview,这下好了,布局出来了,放在机子上跑,卡得想死有木有,信息乱跑乱出现,表示非常 ...

  4. ScrollView中嵌套ListView时,listview高度显示的问题

    方法一:直接更改listview的控件高度,动态获取(根据条目和每个条目的高度获取) 前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个Lis ...

  5. ScrollView中嵌套ListView的问题

    网上关于怎样在ScrollView中嵌套ListView的讨论有很多,我大概是搜索了一下,简单总结如下: 1.不要在ScrollView中嵌套ListView a.用一个LinearLayout来代替 ...

  6. 解决ScrollView中嵌套ListView滚动效果冲突问题

    在ScrollView中嵌套使用ListView,ListView只会显示一行到两行的数据.起初我以为是样式的问题,一直在对XML文件的样 式进行尝试性设置,但始终得不到想要的效果.后来在网上查了查, ...

  7. ScrollView中嵌套ListView显示

    想要ScrollView中嵌套显示ListView 需要自定义ListView 并重写onMeasure方法 重新计算  heightMeasureSpec的高度 int newHeight = Me ...

  8. ScrollView中嵌套ListView

    scrollview中嵌入listview,要是直接把listview嵌进scrollview中,listview的高度是固定的不能进行滑动.默认情况下Android是禁止在ScrollView中放入 ...

  9. Android ScrollView中嵌套ListView

    由于要做一个相似美团的团购产品.scrollview中还有嵌入listview,要是直接把listview嵌进scrollview中.listview的高度是固定的不能进行滑动.默认情况下Androi ...

随机推荐

  1. 如何下载github项目中的部分文件(文件夹)

    https://minhaskamal.github.io/DownGit/#/home 将你要下载的链接放进去即可.

  2. Jmeter-测试计划元件

    打开Jmeter页面,默认显示测试计划和工作台: 1.测试计划: 用来描述一个性能测试,包含与本次性能测试所有相关的功能.也就说性能测试的所有内容是于基于一个计划的. 右键单击"测试计划&q ...

  3. Java位向量的实现原理与巧妙应用

    Java位向量的巧实现原理与巧妙应用 1.博文介绍 本篇博文将会介绍几本的位运算含义.位向量介绍.BitSet实现原理.Java位向量的应用.拓展介绍Bloom Filter等. 2.位运算介绍 1) ...

  4. 动态代理的两种实现方式(JDK/Cglib)

    =========================================== 原文链接: 动态代理的两种实现方式(JDK/Cglib) 转载请注明出处! ================== ...

  5. 【Electron】Electron开发入门

    Electron简介: Electron提供了丰富的本地(操作系统)的API,使你能够使用纯JavaScript来创建桌面应用程序,并且跨平台(win,mac,linux等各种PC端平台).与其它各种 ...

  6. 【转】一个小工具类,利用shareObject把数据缓存

    原文链接:http://bbs.9ria.com/thread-284082-1-2.html 之前做一个数据缓存,就顺便把写入缓存,清除缓存,获取缓存都整理了一下,其中也做了些参考,个人水平有限,有 ...

  7. poptest老李谈数据库优化总结

    poptest老李谈数据库优化总结   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:9088 ...

  8. 老李案例分享:MAT分析应用程序服务出现内存溢出过程

    老李案例分享:MAT分析应用程序服务出现内存溢出过程   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的loa ...

  9. “this kernel requires an x86-64 CPU, but only detects an i686 CPU, unable to boot” 问题解决

    1. 问题描述:  在Virtual Box上安装 Ubuntu 系统时出现错误(如题),VIrtual Box 上也没有64位操作系统的选项 2.原因分析: (1) 可能 BIOS 的 Virtua ...

  10. JDBC基础学习(六)—数据库连接池

    一.数据库连接池介绍 1.数据库连接池的缘由      对于一个简单的数据库应用,由于对于数据库的访问不是很频繁.这时可以简单地在需要访问数据库时,就新创建一个连接,用完后就关闭它,这样做也不会带来什 ...