ScrollView中嵌套ListView的时候只能显示一个条目.
解决:通过自定义ListVIew,并对ListView的高度进行重新计算
package com.example.dk.text1; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ListView; import android.widget.ScrollView; import android.widget.TextView; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.util.ArrayList; import java.util.logging.Level; public class MainActivity extends AppCompatActivity { private ArrayList<String> mDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView lv = (ListView) findViewById(R.id.lv); MyAdapter adapter = new MyAdapter(); lv.setAdapter(adapter); initData(); } private void initData() { mDatas = new ArrayList<String>(); for (int i = 0; i < 100; i++) { mDatas.add("当前条目为 "+i); } } class MyAdapter extends BaseAdapter{ @Override public int getCount() { return mDatas==null?0:mDatas.size(); } @Override public Object getItem(int position) { return mDatas==null?null:mDatas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder ; if (convertView==null) { convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item,null); holder = new ViewHolder(convertView); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.mItemTV.setText(mDatas.get(position)); holder.mItemBtn.setText(mDatas.get(position)); return convertView; } class ViewHolder { private final TextView mItemTV; private final Button mItemBtn; public ViewHolder(View convertView) { mItemTV = (TextView) convertView.findViewById(R.id.item_tv); mItemBtn = (Button) convertView.findViewById(R.id.item_btn); } } } 布局<?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:orientation="vertical" tools:context="com.example.dk.text1.MainActivity"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff5500" > <com.example.dk.text1.MyListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff005555" > </com.example.dk.text1.MyListView> </ScrollView> </LinearLayout>//自定义ListViewpackage com.example.dk.text1; import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; /* * @项目名: Text1 * @包名: com.example.dk.text1 * @文件名: MyListView * @创建者: D.K * @创建时间: 2016/12/1 19:14 * @描述: TODO */ public class MyListView extends ListView { public MyListView(Context context) { super(context); } public MyListView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heigh = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heigh); } }