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

时间:2015-09-16 05:23:52

标签: android android-layout scrollview

我在一个activity中使用了两个Scroll视图。每个滚动视图内部添加表格布局。每个表格布局从我的数据库值以编程方式添加记录。我需要在每个表格中一次添加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>

1 个答案:

答案 0 :(得分:0)

如果我按照你的问题,我认为你需要修复滚动视图高度。滚动视图可以不止一个..在这种情况下,将ScrollView放入 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>