2010-09-12 58 views
0

我正在寫一個android應用程序,並且在其中我想要有一個標題欄,包括遊戲名稱,分數等等。然後在下面我想要一個圖像視圖的網格,這將有點像我可以在遊戲中顯示不同角色的瓦片。Android版面不能正常顯示

我有以下XML佈局,但是當我運行它,我得到一個黑色的屏幕:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

    <TextView android:id="@+id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="@string/app_name" 
      /> 
    <TextView android:id="@+id/score" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="@string/score" 
      /> 
    <TextView android:id="@+id/score" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="0" 
      /> 
    <TextView android:id="@+id/robotsLeft" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="@string/robots" 
      /> 
    <TextView android:id="@+id/score" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="0" 
      /> 

</LinearLayout> 

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:columnWidth="90dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:gravity="center" 
/> 

GridView控件初始化爲每本教程:

http://developer.android.com/resources/tutorials/views/hello-gridview.html

即。在我的主要活動onCreate():

super.onCreate(savedInstanceState); 
setContentView(R.layout.main);   
GridView gridview = (GridView) findViewById(R.id.gridview); 
gridview.setAdapter(new ImageAdapter(this)); 

任何人都可以看到我做錯了什麼嗎?

謝謝!

回答

2

那麼,這是無效的XML,因爲沒有根元素。此外,您所有的TextView小工具都有android:layout_height="fill_parent",這很奇怪。你也可以考慮使用hierarchyviewer來更仔細地檢查你的佈局,看看還有什麼可能會出錯。

+0

感謝您的回答,我只是愚蠢的,並有我的遊戲循環開始形式onCreate(),但佈局直到onCreate()後才呈現我猜?無論如何,通過使我的班級與遊戲循環解決Runnable – Kalypso 2010-09-12 19:59:18

+0

@Kalypso解決方案:正確 - 直到您將控制權交還給Android,您的用戶界面將不會呈現。 – CommonsWare 2010-09-12 20:11:02

0

您應該在它們下方放置一個(線性)佈局,因爲您的xml無效。