如何以编程方式为具有布局高度换行内容的滚动视图设置最大高度



我在一个活动中使用了两个滚动视图。每个滚动视图内部添加表布局。每个表布局以编程方式从我的数据库值添加记录。我需要在每个表中添加10条记录,每次滚动视图高度在包裹内容和超过50条记录添加到表滚动视图最大高度设置在500。我该怎么做呢?请帮帮我。遵循我的XML代码

main_activity.xml

<ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="fill_parent"
    android:layout_height="450dp"
    android:layout_alignParentRight="true">
    <TableLayout
          android:id="@+id/main_table" 
          android:layout_width="fill_parent"    
          android:layout_height="wrap_content" 
          android:stretchColumns="0,1" 
          android:layout_column="0"
          android:layout_weight="1.0" 
          android:layout_below="@+id/relativeLayout" 
          android:layout_alignParentLeft="true"
        xmlns:android="http://schemas.android.com/apk/res/android" />
</ScrollView>
<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/req_head"
    android:layout_below="@+id/scrollView2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="10dp"></TableLayout>
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView4"
    android:layout_below="@+id/req_head"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="0dp">
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/maintable2">
    </TableLayout>
</ScrollView>

如果我遵循你的问题,我认为你需要修复滚动视图的高度。滚动视图可以不止一个,在这种情况下,把ScrollView放到a中LinearLayout使滚动视图wrap_content的高度,并根据需要固定父LinearLayuot的大小。如:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="450dp"
    android:orientation="vertical" >
      <ScrollView
        android:id="@+id/scrollView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TableLayout
              android:id="@+id/main_table" 
              android:layout_width="fill_parent"    
              android:layout_height="wrap_content" 
              android:stretchColumns="0,1" 
              android:layout_column="0"
              android:layout_weight="1.0" 
              android:layout_alignParentLeft="true" />
       </ScrollView>
</LinearLayout>

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/req_head"
    android:layout_marginTop="10dp"></TableLayout>
  <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scrollView4">
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/maintable2">
        </TableLayout>
    </ScrollView>
</LinearLayout>    
</LinearLayout>

最新更新