2017-06-23 63 views
0

我在gridview中創建了10x10個網格的Word搜索遊戲。 字母不會完全顯示在每個單元格中。 這裏是Screen shot of gridview with characters filled inAndroid中的Gridview沒有顯示字符

代碼片段如下:

MainActivity.java

公共類MainActivity擴展AppCompatActivity {

GridView gridView; 

static final String[] numbers = new String[] { 
     "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
     "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", 
     "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", 
     "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", 
     "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
     "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", 
     "W", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", 
     "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", 
     "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", 
     "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", 
}; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    gridView = (GridView) findViewById(R.id.gridView1); 

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, numbers); 

    gridView.setAdapter(adapter); 

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     public void onItemClick(AdapterView<?> parent, View v, 
           int position, long id) { 
      Toast.makeText(getApplicationContext(), 
        ((TextView) v).getText(), Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 

android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 

android:id="@+id/gridView1" 
android:numColumns="10" 
android:gravity="center" 
android:columnWidth="50dp" 
android:stretchMode="columnWidth" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

tools:context="in.viyanservices.wordsearch1.MainActivity"> 
</GridView> 

我跟着這個教程:http://www.mkyong.com/android/android-gridview-example/。他使用了android:numColumns =「auto_fit」。我正在使用android:numColumns =「10」

您的迴應將不勝感激!非常感謝!

回答

0

更新您的GridView XML

<?xml version="1.0" encoding="utf-8"?> 
    <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     tools:context="in.viyanservices.wordsearch1.MainActivity" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
    <GridView 
     android:id="@+id/gridView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:numColumns="10"/> 
    </HorizontalScrollView> 
+0

我想看到整個電網在一個屏幕。你的建議會讓用戶水平滾動。對? –

+0

無法在小分辨率設備的單屏中顯示10列。 –

1

只要改變你的XML屬性

android:stretchMode="spacingWidth" 
+0

android:stretchMode =「spacingWidth」當我設置columnWidth =「50dp」時工作。 但是,當columnWidth設置爲「30dp」時,會發生同樣的問題。字母未完整顯示。 –

+0

如果你想減少列的大小,你必須減少gridview內的textview的大小......因爲你必須使用你自定義的textview ... –