2011-06-10 19 views
1

我想整理我有以下视图控件视图 -Android的视图定制问题

  • 的TextView(应该充满屏幕的宽度并具有特定高度)
  • 自定义视图绘制使用绘图( )(填满宽度,应该在上面的文本视图之后开始并填充屏幕)

我想写一个棋盘游戏,因此绘制自定义视图。

我的问题是什么是最好的布局使用这个?

我可以使用TableLayout并将这两个视图分别作为一列吗?我认为这样做可以做到这一点,但是我使用自定义视图的布局并不会填满屏幕,即使我正在绘制的表格填满了屏幕。

我希望我能够正确解释自己。任何指针都非常感谢。

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

     <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/tl_question" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:shrinkColumns="1" 
      android:stretchColumns="2"> 

      <TableRow> 
       <TextView 
        android:id="@+id/question" 
        android:layout_width="wrap_content" 
        android:textSize="30sp" 
        android:textColor="#ffFFFFFF" 
        android:layout_gravity="center_vertical" 
        android:paddingBottom="9dip" 
        android:layout_marginLeft="3dip" 
        android:layout_marginRight="2dip"/> 
      </TableRow> 

      <TableRow> 
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:layout_gravity="center_vertical" > 
        <com.ac.gui.CustomView 
         android:id="@+id/custom_board" 
         android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
         android:keepScreenOn="true" /> 
       </LinearLayout> 
      </TableRow> 
     </TableLayout> 

</RelativeLayout> 

回答

0

尝试使用此

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


      <TextView 
       android:id="@+id/question" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="30sp" 
       android:text="ASDFASDF" 
       android:textColor="#ffFFFFFF" 
       android:layout_gravity="center_vertical" 
       android:paddingBottom="9dip" 
       android:layout_marginLeft="3dip" 
       android:layout_marginRight="2dip"/> 

      <com.ac.gui.CustomView 

      android:layout_below="@+id/question" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" /> 

</RelativeLayout> 

我测试代替你的一个不同的布局,它工作正常。它也应该和你一起工作。

RelativeLayout允许您定位项目,以及相对于彼此。 您会注意到您的自定义视图中的属性“layout_below”,其中我指定要放置在其下的视图的ID。

+0

感谢您的回复。这很有道理。我一直在想。如果我想在自定义视图下添加另一个文本视图或按钮,我该怎么办?理想情况下,我不想修复自定义视图的高度。所以我希望它是相对于它下面的textview的高度。可以这样做吗? – user220201 2011-06-10 03:58:14

+0

当然!你想要做的是将TextView放在ABOVE自定义视图的XML中。确保给TextView一个Id。然后,在您的CustomView中,添加属性android:layout_above =“@ id/whateveryounamedit”。现在您的自定义视图将显示在问题文本视图的下方,并位于新文本视图的上方! :)您应该将TextView放置在自定义视图之前,因为XML是内联读取的。因此,如果将layout_above属性放在CustomView下面的TextView中,它不会知道您引用的资源(这可能已在更新的Eclipse插件中修复) – dymmeh 2011-06-11 13:22:54