2013-02-15 128 views
4

我正在爲Android應用程序設計佈局。這是佈局的骨架。 enter image description here無法將視圖投射到Android佈局中的ViewGroup中

我試過的是使用表格佈局,因爲GUI可以分解成表格行和列。 基本上,我想把佈局分成兩列,就像你看到的那樣。 下面是XML代碼

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <!-- 2 columns --> 
    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dip" > 

     <View 
      android:id="@+id/view1" 
      android:layout_weight="7" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/textView1" 
       android:text="Column 1" 
       android:textAppearance="?android:attr/textAppearanceLarge" /> 
     </View> 

     <View 
      android:id="@+id/view2" 
      android:layout_weight="3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" > 
      <Button 
       android:id="@+id/button1" 
       android:text="Column 2" /> 
     </View> 

    </TableRow> 


</TableLayout> 

我得到的GUI

Exception raised during rendering: android.view.View cannot be cast to android.view.ViewGroup 

下面的錯誤,我認爲這表示我們不能組的意見。如果不可能,該如何處理。我來自網頁背景。我以爲我可以使用視圖作爲像HTML一樣的div。

回答

8

您可以將視圖分組,但您需要使用ViewGroup而不是視圖。 (使用相對位置顯示子視圖),FrameLayout,TableLayout等。使用ViewGroup適合您需要的任何ViewGroup。

爲了得到一些工作,將XML中的兩個視圖更改爲FrameLayouts。同時給出Button和TextView佈局參數(例如android:layout_width="wrap_content",高度相同) - 否則在運行時會出現另一個錯誤。

相關問題