2012-12-30 25 views
4

我使用一個GridView,顯示與背景圖像加上文字的LinearLayout,就像這個例子:的GridView行高行爲不正確

http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/

就這樣很例子,我使用該代碼使圖像具有圓角。

問題是,如果我不使用圓角的東西,所有我的網格物品都是相同的高度,不管他們有什麼文字,即使我的所有圖片都是相同的大小。

但是,如果我使用它,項目的高度被包裹到內容。

我試着定義我的線性佈局wrap_content,fill_parent,match_parent,甚至固定的高度,但系統只是忽略了我。

這是我的網格佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFDDDDDD" 
    android:orientation="vertical" 
    tools:context=".RouteListActivity" > 

    <GridView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/gridview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_margin="20dp" 
     android:layout_weight="2" 
     android:animateLayoutChanges="false" 
     android:clipChildren="false" 
     android:clipToPadding="false" 
     android:drawSelectorOnTop="true" 
     android:gravity="center" 
     android:horizontalSpacing="10dp" 
     android:numColumns="2" 
     android:scrollingCache="false" 
     android:stretchMode="columnWidth" 
     android:verticalSpacing="10dp" /> 

</LinearLayout> 

,這是我的網格項:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearlayoutGridLabel" 
    android:layout_width="wrap_content" 
    android:layout_height="150dp" 
    android:layout_weight="1" 
    android:gravity="bottom|left" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/id_ruta" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     android:visibility="gone" /> 

    <TextView 
     android:id="@+id/routenameGridLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="20dp" 
     android:background="#77000000" 
     android:gravity="bottom" 
     android:padding="5dp" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 

再加上我的適配器getView:

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     View vi=convertView; 
     if(convertView==null) 
      vi = inflater.inflate(R.layout.grid_item, null); 

     LinearLayout linLayout = (LinearLayout) vi.findViewById(R.id.linearlayoutGridLabel); 
     Typeface yanone = Typeface.createFromAsset(mContext.getAssets(), "YanoneKaffeesatz-Regular.ttf"); 
     TextView id_ruta = (TextView) vi.findViewById(R.id.id_ruta); 
     TextView nombre=(TextView)vi.findViewById(R.id.routenameGridLabel); 
     nombre.setTypeface(yanone); 
     nombre.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 38); 
     Ruta current = routeList.get(position); 
     try { 
//   Drawable d; 
//   d = Drawable.createFromStream(mContext.getAssets().open("ruta" + (position+1) + "/title.jpg"), null); 
      Bitmap b = BitmapFactory.decodeStream(mContext.getAssets().open("ruta" + (position+1) + "/title.jpg")); 
      StreamDrawable d = new StreamDrawable(b, 10, 0); 
      linLayout.setBackgroundDrawable(d); 
     } catch (IOException e) { 
      linLayout.setBackgroundResource(R.drawable.noimage); 
     } 
     nombre.setText("" + current.getNombre()); 
     id_ruta.setText(current.getId().toString()); 

     return vi; 
    } 

坦率地說,我不瞭解gridviews行爲。以前,我將不同尺寸的圖像設置爲背景圖像,並且所有元素都相互重疊。即使在網格項目的佈局上固定高度,我也無法制作具有相同大小的行。

我可以在這裏得到一些指示嗎?

回答

31

我終於解決了它,原來的問題是,我用

vi = inflater.inflate(R.layout.grid_item, null); 

代替

vi = inflater.inflate(R.layout.grid_item, parent, false); 

所以佈局參數被忽略。

無論如何,讓GridView均勻分佈空間是一個痛苦的屁股。我想我會切換到TableView。

+1

曾嘗試android:stretchMode =「columnWidth」? –

+1

Ohhh MG..you救了我3小時33分42秒..謝謝:D –