【问题标题】:Android - GridView height to max heightAndroid - GridView 高度到最大高度
【发布时间】:2012-03-23 20:26:28
【问题描述】:

我在滚动视图中有一个 GridView,并且 GridView 中加载了 4 个图像(通过 ImageAdapter)。我的问题是我可以显示 3 张图片,但第 4 张没有。经过进一步调查,我发现gridview的高度只是行的高度,所以如果我将xml中的gridview高度设置为700dp,那么它就会显示出来。

截图如下:

如您所见,我将 GridView 的背景设置为 HotPink 以说明 GridView 的位置。

这是 main_menu 的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llLayoutContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >    

    <!-- <LinearLayout android:id="@+id/llMiddle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"> -->

        <LinearLayout
            android:id="@+id/llLeft"
            android:layout_width="400dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@drawable/layout_left"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"> 
            <ScrollView 
                android:id="@+id/svMenu"
                android:layout_width="400dp"
                android:layout_height="match_parent"
                >
            </ScrollView>           
        </LinearLayout>

        <LinearLayout
            android:id="@+id/llRight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:background="@drawable/layout_right"
            android:padding="0dp"> 
        </LinearLayout>
    <!-- </LinearLayout> -->
</LinearLayout>

LinearLayout“llLeft”是加载菜单项的布局(在 ScrollView 内)。 layout_left 只是一个在绿色背景周围绘制深色轮廓的形状。

这是包含 GridView 的 main_menu_header.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="5dp">

    <TextView
             android:id="@+id/tvDashboardHeader"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:text="Dashboard Main Menu"
             android:textSize="20dp"
             style="@style/TextShadow"
             android:paddingTop="5dp"
             android:gravity="left"
             android:background="@drawable/menu_item_bg"        
            />
    <GridView 
        android:id="@+id/gvMenuItems"
        android:layout_width="400dp"
        android:layout_height="match_parent"
        android:columnWidth="110dp"
        android:numColumns="auto_fit"    
        android:verticalSpacing="10dp"    
        android:horizontalSpacing="10dp"    
        android:stretchMode="columnWidth"
        android:background="@color/HotPink"
        android:gravity="center" >
    </GridView>

</LinearLayout>

这是我填充 GridView 的方式:

        ScrollView sv = (ScrollView)findViewById(R.id.svMenu);
        LayoutInflater liInflater = getLayoutInflater();
        ViewGroup vg = (ViewGroup)liInflater.inflate(R.layout.main_menu_header, sv, false);
        sv.addView(vg);

        //Setup the Main Menu items on the left side.
        GridView gvMenuItems = (GridView) findViewById(R.id.gvMenuItems);    
        gvMenuItems.setAdapter(new ImageAdapter(this));

ImageAdapter 类:

ImageAdapter 类:

public class ImageAdapter extends BaseAdapter {
    private Context mContext;    

    public ImageAdapter(Context c) {
        mContext = c;    
    }    

    public int getCount() {        
        return mThumbIds.length;    
    }    

    public Object getItem(int position) {        
        return null;    
    }    

    public long getItemId(int position) {        
        return 0;    
    }    

    // create a new ImageView for each item referenced by the Adapter    
    public View getView(int position, View convertView, ViewGroup parent) {        

        ImageView imageView;

        if (convertView == null) {
            // if it's not recycled, initialize some attributes            
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(parent.getLayoutParams().width, 125)); //new GridView.LayoutParams(400, 125));             
            //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setMaxHeight(50);
            imageView.setMaxWidth(50);
            //imageView.setPadding(30, 0, 0, 0);
            //imageView.setPadding(40, 30, 20, 30);

            } else {            
                imageView = (ImageView) convertView;        
            }        

        imageView.setImageResource(mThumbIds[position]);

        return imageView;
    }    

    // references to our images    
    private Integer[] mThumbIds = {
            R.drawable.home,
            R.drawable.open_folder, 
            R.drawable.paper_pen,
            R.drawable.person
    };
}

我认为 android:layout_height="match_parent" 的 gridview 属性会起作用,但事实并非如此。关于如何让 GridView 在滚动视图内占据整个左侧的任何想法?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    我最好的猜测是scrollview 给你带来了问题。

    你为什么需要scrollview

    Grid View 将处理滚动。这有点多余。所以请删除它并直接将膨胀视图添加到llLeft。那将解决问题。


    其实用xml来解决你的UI问题会更优雅。在main_menu.xml 中使用&lt;include&gt; 标签。方法如下

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llLayoutContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >    
    
    <!-- <LinearLayout android:id="@+id/llMiddle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"> -->
    
        <LinearLayout
            android:id="@+id/llLeft"
            android:layout_width="400dp"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@drawable/layout_left"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"> 
            <include
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             layout="@layout/main_menu_header" />           
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/llRight"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            android:background="@drawable/layout_right"
            android:padding="0dp"> 
        </LinearLayout>
    <!-- </LinearLayout> -->
    

    【讨论】:

    • 你说得对……scrollview 是多余的……对xml 进行更改后,我是否只是将gridview 适配器设置为ImageAdapter?我试过了,但什么都没有出现,我猜是因为 ImageAdapter 不会放大视图但不太确定。我的大脑是一个有点垃圾的自动取款机。如何直接为视图充气?
    • 如果您遵循我建议的 xml,您只需要在问题中使用最后两行代码即可。 //设置左侧的主菜单项。 GridView gvMenuItems = (GridView) findViewById(R.id.gvMenuItems); gvMenuItems.setAdapter(new ImageAdapter(this));
    • 你可以试试你的老方法。只需删除 scroll-view 。它也应该可以工作。
    • 是的,我尝试仅添加包含,然后仅添加以下内容: GridView gvMenuItems = (GridView) findViewById(R.id.gvMenuItems); gvMenuItems.setAdapter(new ImageAdapter(this));但现在什么也没有出现。是因为我实际上并没有在 ImageAdapter 类中膨胀任何东西吗?我已将其添加到上述帖子中以供参考。
    • 仅供参考,这就是问题所在......我没有在适配器中放大视图(我也不再在活动中这样做)......现在它可以工作了。再次感谢您的帮助和提示,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 2018-09-13
    • 2014-02-27
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多