2015-05-14 63 views
2

我是新開發Android應用程序的第一個應用程序,它是一個項目的wordsearch應用程序,我使用GridView來顯示搜索字母。我可以使用佈局xml來更改網格中單元格的寬度,但是我無法改變高度,此刻它太多了。在gridview中更改行高

我試着谷歌的答案如何做到這一點,但我發現的一切都是關於重寫自定義arrayAdapter中的getView,而且我並不真正遵循如何做到這一點,或者在做什麼時如何將高度設置爲固定值它。我在佈局文件中將行寬設置爲25dp,並且想要更改高度以符合該高度,但似乎無法找到任何聯機的方式。

如果您可以提供幫助,請提前致電。

編輯:這裏是我填的是網格:

//fill GridView with the puzzle input array from the puzzle class 
ArrayAdapter<String> gridAdapter = new ArrayAdapter<String>(c, R.layout.cell_layout, todaysPuzzle.puzzleInputArray); 
wordsearchGrid.setAdapter(gridAdapter); 

//fill ListView with the searchWords array from the puzzle class 
ArrayAdapter<String> wordsAdapter = new ArrayAdapter<String>(c, R.layout.cell_layout, todaysPuzzle.searchWords); 
wordsList.setAdapter(wordsAdapter); 

這裏是爲GridView的佈局(我用兩個,一個是單詞搜索和一個存儲搜索詞:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@android:color/black" 
tools:context="little.keith.wordsearch.TodaysActivity" > 

<GridView 
    android:id="@+id/wordsearchGrid" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:layout_margin="4dp" 
    android:background="@android:color/black" 
    android:columnWidth="25dp" 
    android:gravity="top" 
    android:horizontalSpacing="2dp" 
    android:numColumns="12" 
    android:stretchMode="none" 
    android:verticalSpacing="2dp" > 
</GridView> 

<GridView 
    android:id="@+id/wordsList" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_margin="4dp" 
    android:background="@android:color/black" 
    android:numColumns="auto_fit" 
    android:layout_below="@+id/wordsearchGrid" 
    android:horizontalSpacing="2dp" 
    android:verticalSpacing="2dp" 
    android:layout_centerHorizontal="true" > 
</GridView> 

這裏是cell_layout.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/text1" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@android:color/white" 
android:gravity="center" 
android:minHeight="?android:attr/listPreferredItemHeight" 
android:paddingStart="1dp" 
android:paddingEnd="1dp" 
android:textAppearance="?android:attr/textAppearanceLarge" /> 
+0

歡迎JasperMoneyShot,你可以請編輯問題並添加您用於GridView的佈局xml?這會讓人們有機會看到你可能會出錯的地方,並且更容易提出建議。 – CodeMonkey

+0

你看過這個[documentation](http://developer.android.com/guide/topics/ui/layout/gridview.html)嗎? – Kumiho

+0

CodeMonkey:好的,謝謝。 Kumiho:是的,我看過它,但無法弄清楚如何讓它做我想做的事,當我嘗試它時,謎題根本沒有顯示。 – JasperMoneyshot

回答

1

管理弄清楚如何自己做到這一點,只是堅持答案,以防其他人陷入同樣的​​問題。要解決此行高度,我寫這讓我與setLayoutParams定義單元尺寸的定製ArrayAdapter:

public class GridAdapter extends ArrayAdapter 
{ 
    Context context; 
    Object[] items; 
    int resource; 

    LayoutInflater inflater; 

    public GridAdapter(Context context, int resource, Object[] items) 
    { 
     super(context, resource, items); 
     this.context = context; 
     this.resource = resource; 
     this.items = items; 

     inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() 
    { 
     return items.length; 
    } 

    @Override 
    public Object getItem(int position) 
    { 
     return items[position]; 
    } 

    @Override 
    public long getItemId(int position) 
    { 
     return position; 
    } 

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

     int cellWidth = 40; 
     int cellHeight = 50; 


     if (convertView == null) 
     { 
      // If convertView is null then inflate the appropriate layout file 
      convertView = inflater.inflate(resource, null); 
     } 

     cell = (TextView) convertView.findViewById(R.id.gridCell); 

     cell.setText((CharSequence) items[position]); 

     // Set height and width constraints for the image view 
     cell.setLayoutParams(new LinearLayout.LayoutParams(cellWidth, cellHeight)); 

     // Set Padding for images 
     cell.setPadding(1, 1, 1, 1); 

     return convertView; 
    } 
} 

然後說這就是在活動:

//fill GridView with the puzzle input array from the puzzle class 
GridAdapter gridAdapter = new GridAdapter(c, R.layout.cell_layout, todaysPuzzle.puzzleInputArray); 
wordsearchGrid.setAdapter(gridAdapter);