先看看布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/sv_add_contain"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="服务器"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="国网天津电力"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="资源池优化"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="服务器"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="国网天津电力"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="资源池优化"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="奇瑞"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="未出库"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="请输入序列号"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="请输入设备型号"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="日期 2017-03-17"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="日期 2017-03-17"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<EditText
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="请输入备注"
android:maxLines="3"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<ListView
android:id="@+id/lv_add_device"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="扩展属性"
android:layout_marginTop="10dp"
android:background="@drawable/round_shape"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入"
android:paddingLeft="10dp"
android:background="@drawable/round_shape"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="单行输入"
android:paddingLeft="10dp"
android:background="@drawable/round_shape"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_below="@+id/sv_add_contain"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_add_code"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="生成二维码"
android:background="@drawable/round_shape"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="确定"
android:background="@drawable/round_shape"/>
</LinearLayout>
</LinearLayout>
布局预览
解决的两种方法
第一种 自定义ListView
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;
/**
* Created by Linsa on 2017/3/17:16:14.
* des:
*/
public class ScrollListView extends ListView {
public ScrollListView(Context context) {
this(context,null);
}
public ScrollListView(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}
public ScrollListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
第二种 自己测量ListView的总高度,在设置完Adapter后调用这个方法就可以重新测量和绘制ListView的高度,具体的原理可以参考View的绘制和测量过程 (这个方法可以直接使用)
public void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
运行截图