ScrollView嵌套GridView或ListView,由于这两款控件都自带滚动条,当他们碰到一起的时候便会出问题,即GridView会显示不全。
解决办法:自定义一个GridView控件
package cn.com.gxlu.frame.impl; import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView; public class MyGrideView extends GridView { public MyGrideView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} public MyGrideView(Context context, AttributeSet attrs) {
super(context, attrs);
} public MyGrideView(Context context) {
super(context);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
该自定义控件只是重写了GridView的onMeasure方法,使其不会出现滚动条,ScrollView嵌套ListView也是同样的道理,不再赘述。 XML布局代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dddddd" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> <cn.com.gxlu.frame.impl.MyGrideView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="35dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:horizontalSpacing="1dip"
android:numColumns="3"
android:verticalSpacing="1dip" >
</cn.com.gxlu.frame.impl.MyGrideView>
</LinearLayout> </ScrollView>