开发者

Resize tablerow dynamically

I have a custom listview which changes size when one of the rows is clicked, new rows are added. When the activity first loads there is only one row and two buttons(previous & next) in a tablelayout. Currently if i have my listview in added to a tablerow & row 2 contains the buttons. The layout height & width are set to wrap_content.

When new rows are added to the listview, the r开发者_Go百科ow size does not change and secondly expands to occupy the space that was for buttons. And therefore as a result my buttons disappear.

main.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>

How do I dynamically update the row size when the new rows are being added to the views.? Thanks


Replace TableLayout with a 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>

This layout will put your buttons at the bottom of the screen, while the ListView will cover the whole screen.


Don't put your CustomList in a table, use linear layout for holding the buttons. Pay particular attention to the layout_weight attributes as shown in the following layout markup

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜