如何计算GridView内容高度(以像素为单位)?

时间:2013-09-23 03:16:17

标签: android height android-gridview android-scrollview

我无法以编程方式获取GridView内容高度,因为我将其置于ScrollView中,我只需要通过在 PIXELS 中获取其内容高度来动态拉伸GridView高度。这就是我的所作所为:

@Override 
public void onWindowFocusChanged(boolean hasFocus) 
{ 
     // TODO Auto-generated method stub 
     super.onWindowFocusChanged(hasFocus);
     System.out.println("...111Height..."+gridView.getMeasuredWidth());
}

但我希望获取OnCreate内的值,以便我可以将其提供给layoutParams.height

GridView gridView = (GridView) findViewById(R.id.gridview_module);
    ViewGroup.LayoutParams layoutParams = gridView.getLayoutParams();
    layoutParams.height = convertDpToPixels(iDontKnowWhatToPutHere, this);
    gridView.setLayoutParams(layoutParams);

以及从像素转换为dp的方法:

public static int convertDpToPixels(float dp, Context context) {
    Resources resources = context.getResources();
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
            resources.getDisplayMetrics());
}

修改

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".SecondActivity" >

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="0"
    android:layout_weight="1"
    android:fillViewport="true"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <com.example.ExpandableHeightGridView
            android:id="@+id/gridview_module"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@id/tv"
            android:layout_weight="1"
            android:gravity="center"
            android:horizontalSpacing="20dp"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />

        <Button
            android:id="@+id/dial_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/gridview_module"
            android:onClick="dialPhone"
            android:text="Dial Phone" />
    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

要实现这一目标,您需要像这样创建CustomGridview

public class CustomGridView extends GridView {

    private int old_count;
    private android.view.ViewGroup.LayoutParams params;
    private boolean isExpandFully = false;
    private int verticleSpacing = 4;
    private int noOfCollumns = 1;

    public int getNumColumns() {
        return noOfCollumns;
    }

    public void setNoOfCollumns(int noOfCollumns) {
        this.noOfCollumns = noOfCollumns;
    }

    public int getVerticleSpacing() {
        return verticleSpacing;
    }

    public void setVerticleSpacing(int verticleSpacing) {
        this.verticleSpacing = verticleSpacing;
    }

    public void setExpandFully(boolean isExpandFully) {
        this.isExpandFully = isExpandFully;
    }

    public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public CustomGridView(Context context) {
        super(context);
        init(null);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            String namespace = "http://schemas.android.com/apk/res/android";
            noOfCollumns = attrs.getAttributeIntValue(namespace, "numColumns", 1);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {

        setVerticalSpacing(getVerticleSpacing());
        if (isExpandFully) {
            try {
                if (getCount() != old_count) {
                    old_count = getCount();
                    params = getLayoutParams();

                    int len = (getCount() % getNumColumns() == 0 ? getCount() / getNumColumns() : getCount() / getNumColumns() + 1);
                    params.height = 0;
                    for (int i = 0; i < len; i++) {
                        params.height = params.height + (old_count > 0 ? getChildAt(0).getHeight() + getVerticleSpacing() : 0);
                    }
                    params.height += 10;
                    setLayoutParams(params);
                }
            } catch (Exception e) {
            }
        }
        super.onDraw(canvas);
    }

}

如何使用

CustomGridView myGridview = (CustomGridView)findViewById(R.id.customGridview);

myGridview.setExpandFully(true);