2012-03-23 160 views
0

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

以下是截图:

enter image description here

正如你所看到的,我设置在GridView的背景暗粉红,说明在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” 对应于菜单项得到加载布局(了滚动内)。 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在滚动视图内占据整个左侧的任何想法?

回答

1

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

为什么你需要scrollview

网格视图将处理滚动。这是多余的。所以请删除它,直接添加充气视图直接到llLeft。这将解决问题。


事实上,使用xml解决你的UI问题会更优雅。在您的main_menu.xml中使用<include>标记。下面是如何

<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> --> 

+0

你说的没错......滚动型是多余的....进行更改的XML后,我只需设置gridview的适配器连接到ImageAdapter?我试过这个,但没有出现,我猜是因为ImageAdapter不会夸大视图,但不太确定。我的大脑是一个小小的自动取款机。我如何直接膨胀视图? – Robert 2012-03-23 03:20:14

+0

你只需要在你的问题中使用最后两行代码,如果你遵循我建议的xml。 //设置左侧的主菜单项。 GridView gvMenuItems =(GridView)findViewById(R.id.gvMenuItems); gvMenuItems.setAdapter(new ImageAdapter(this)); – PH7 2012-03-23 05:30:35

+0

你可以尝试你的老方法。只需删除滚动视图。它也应该工作。 – PH7 2012-03-23 05:31:07