Android布局问题 - 使用权重的相对宽度百分比

时间:2010-05-26 16:10:21

标签: android layout

我正在尝试使用建议here中的layout_weight为TabHost中的ListView中的列指定相对宽度:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <LinearLayout
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
     <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
        <FrameLayout android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent">
           <TableLayout  
              android:id="@+id/triplist" 
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:paddingTop="4px">
              <TableRow>
                 <ListView android:id="@+id/triplistview"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"/>
              </TableRow>
              <TableRow>
                 <Button android:id="@+id/newtripbutton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Add Trip"/>
              </TableRow>
[other tabs ...]

我的行定义有4列,我想按如下方式调整大小:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:weightSum="1.0"
   android:padding="4px">   
   <TextView android:id="@+id/rowtripdate"
      android:layout_weight=".2"
      android:layout_width="0dip"
      android:layout_height="wrap_content"
      android:inputType="date"/>
   <TextView android:id="@+id/rowodostart"
      android:layout_weight=".2"
      android:layout_width="0dip"
      android:layout_height="wrap_content"/>
   <TextView android:id="@+id/rowodoend"
      android:layout_weight=".2"
      android:layout_width="0dip"
      android:layout_height="wrap_content"/>
   <TextView android:id="@+id/rowcomment"
      android:layout_weight=".4"
      android:layout_width="0dip"
      android:layout_height="wrap_content">

不幸的是,它似乎想要将所有列放入按钮占用的空间,而不是屏幕的宽度。或许还有另一个我不理解的约束。我很感激你的帮助。

alt text http://heeroz.com/layoutprob.png

2 个答案:

答案 0 :(得分:11)

我想我忘记了TableLayout元素的这个属性:

android:stretchColumns="0"

现在似乎正在运作。

答案 1 :(得分:1)

我没有看到你在那里展示的任何明显错误。为了更好地了解正在发生的事情,您可以试用heirarchy viewer。它附带SDK。它将为您呈现每个“隐藏”层所占用的空间,因此您可以确定哪一个决定只有那么大。

相关问题