0

我有一个自定义列表视图,当单击其中一行时,它会改变大小,添加新行。当活动首次加载时,表格布局中只有一行和两个按钮(上一个和下一个)。目前,如果我将列表视图添加到 tablerow 并且第 2 行包含按钮。布局高度和宽度设置为 wrap_content。

当新行添加到列表视图时,行大小不会改变,然后会扩展以占据按钮的空间。因此,我的按钮消失了。

主要的.xml

<LinearLayout android:orientation="vertical" style="?fill_parent"
android:layout_weight="1">
<TableLayout style="?fill_parent" android:layout_weight="1" >
<TableRow android:id="@+id/tr1">
   <CustomList android:id="@+id/mainview" ndroid:layout_height="wrap_content"
   android:layout_width="wrap_content" />
</TableRow>
<TableRow android:layout_height="wrap_content" android:layout_width="match_parent">
   <Button android:layout_width="wrap_content" android:id="@+id/button1"
    android:text="@string/buttonPrevious" android:layout_height="wrap_content">         </Button>
  <Button android:layout_width="wrap_content" android:id="@+id/button2"
  android:text="@string/buttonNext" android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>
</LinearLayout>

将新行添加到视图时,如何动态更新行大小。?谢谢

4

2 回答 2

1

将 TableLayout 替换为 RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <CustomList android:layout_above="@+id/btn_pnl" 
    android:id="@+id/mainview" 
    android:layout_height="fill_parent"
    android:layout_width="fill_parent" />
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/btn_pnl"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:layout_alignParentBottom="true">
        <Button android:layout_width="wrap_content" 
          android:id="@+id/button1"
          android:text="@string/buttonPrevious" 
          android:layout_height="wrap_content">
        </Button>
        <Button android:layout_width="wrap_content" 
          android:id="@+id/button2"
          android:text="@string/buttonNext" 
          android:layout_height="wrap_content"></Button>
    </LinearLayout>
</RelativeLayout>

这种布局会将您的按钮放在屏幕底部,而ListView将覆盖整个屏幕。

于 2011-07-22T16:22:27.830 回答
0

不要把你CustomList放在桌子上,使用线性布局来按住按钮。请特别注意layout_weight以下布局标记中显示的属性

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <CustomList android:id="@+id/mainview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/llButtons" android:layout_gravity="bottom" android:layout_weight="0">
        <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
        <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"></Button>
    </LinearLayout>
</LinearLayout>
于 2011-07-22T16:23:47.327 回答