如何在android中显示2个网格视图

时间:2015-10-24 16:41:22

标签: android gridview scrollview

抱歉我的英文不好,我想在android中显示2个网格视图和一个文本视图和图像视图,下面是我的XML代码,任何一个帮助我如何实现

我想在android中显示2个网格视图和一个文本视图和图像视图    

android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">





    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <ImageView 
            android:id="@+id/image"
            android:layout_width="match_parent"
        android:layout_height="100sp"
        android:src="@drawable/ic_launcher" 

            />


        <GridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
          android:layout_below="@+id/image"
            android:columnWidth="90dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="2"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />

     <TextView 
         android:id="@+id/text"
         android:layout_below="@+id/gridview"
       android:layout_width="match_parent"
    android:layout_height="wrap_content"   
       android:text="Treding"  

         />



        <GridView
            android:id="@+id/gridview1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@+id/text"
            android:columnWidth="90dp"
            android:gravity="center"
            android:horizontalSpacing="10dp"
            android:numColumns="2"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />
    </RelativeLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

您应该做的是以下内容: 可扩展高度gridview: 这意味着您向GridView添加的项目越多,它就会扩展得越多。 (这是你需要的)。

现在该怎么做很简单:

首先,您需要创建一个java类:

public class ExpandableHeightGridView extends GridView {

boolean expanded = false;

public ExpandableHeightGridView(Context context)
{
    super(context);
}

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

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

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // This is a definite hack!!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = View.MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                View.MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded)
{
    this.expanded = expanded;
} }

然后在xml布局中:

    android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">





    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <ImageView 
            android:id="@+id/image"
            android:layout_width="match_parent"
        android:layout_height="100sp"
        android:src="@drawable/ic_launcher" 

            />


 <custom.ExpandableHeightGridView
                    android:id="@+id/grid1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="15dp"
                    android:layout_marginTop="5dp"
                    android:horizontalSpacing="10dp"
                    android:isScrollContainer="false"
                    android:numColumns="3"
                    android:stretchMode="columnWidth"
                    android:verticalSpacing="10dp" />


    <TextView 
         android:id="@+id/text"
         android:layout_below="@+id/gridview"
       android:layout_width="match_parent"
    android:layout_height="wrap_content"   
    android:text="Treding"
         />



         <custom.ExpandableHeightGridView
                    android:id="@+id/grid"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="15dp"
                    android:layout_marginTop="5dp"
                    android:horizontalSpacing="10dp"
                    android:isScrollContainer="false"
                    android:numColumns="3"
                    android:stretchMode="columnWidth"
                    android:verticalSpacing="10dp" />
    </RelativeLayout>
</ScrollView>

然后在你的活动中:

grid = (ExpandableHeightGridView) findViewById(R.id.grid);
    grid.setExpanded(true);
grid.setAdapter(your adapter);

**如果您需要进一步的帮助,请告诉我**