Java 在Android布局中使用ListView滚动中间内容
我正在按照以下设计开发android应用程序 我想将滚动视图的高度设置为剩余的屏幕大小(在被页眉、页脚和其他文本占据之后),尽管列表视图中没有任何内容。当项目添加到列表视图(动态)时,我想用相同的方式滚动它。但是现在,当列表视图中没有项目时,我无法设置初始高度 下面是我的布局文件Java 在Android布局中使用ListView滚动中间内容,java,android,listview,scrollview,android-relativelayout,Java,Android,Listview,Scrollview,Android Relativelayout,我正在按照以下设计开发android应用程序 我想将滚动视图的高度设置为剩余的屏幕大小(在被页眉、页脚和其他文本占据之后),尽管列表视图中没有任何内容。当项目添加到列表视图(动态)时,我想用相同的方式滚动它。但是现在,当列表视图中没有项目时,我无法设置初始高度 下面是我的布局文件 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.an
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_color"
tools:context=".HomeActivity"
android:weightSum="1.0" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@color/header_color"
android:paddingBottom="@dimen/header_vertical_margin"
android:paddingLeft="@dimen/header_horizontal_margin"
android:paddingRight="@dimen/header_horizontal_margin"
android:paddingTop="@dimen/header_vertical_margin" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:weightSum="1.0" >
<ImageButton
android:id="@+id/ImageButton03"
android:layout_width="62dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="0.2"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:gravity="left"
android:scaleType="centerInside"
android:src="@drawable/title_left_img" />
<ImageView
android:id="@+id/headerImg"
android:layout_width="139dp"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:scaleType="centerInside"
android:src="@drawable/header_img"
android:layout_margin="5dp" />
<ImageButton
android:id="@+id/ImageButton04"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:scaleType="centerInside"
android:src="@drawable/title_right_img" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/relativeLayout1"
android:paddingBottom="@dimen/body_vertical_margin"
android:paddingLeft="@dimen/body_horizontal_margin"
android:paddingRight="@dimen/body_horizontal_margin"
android:paddingTop="@dimen/body_vertical_margin" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<TextView
android:id="@+id/dayText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textColor="@color/day_color"
android:textSize="@dimen/day_size"
android:textStyle="bold" />
<TextView
android:id="@+id/dateTimeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView | TextView"
android:textColor="@color/time_date_color"
android:textSize="@dimen/date_time_size"
android:textStyle="normal" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="0px"
android:background="@drawable/rounced_rect"
android:layout_weight="1"
android:fillViewport="true" >
<ListView
android:id="@+id/list_tracking"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp"
android:scrollingCache="true" >
</ListView>
</ScrollView>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="1dp"
android:paddingBottom="@dimen/footer_vertical_margin"
android:paddingLeft="@dimen/footer_horizontal_margin"
android:paddingRight="@dimen/footer_horizontal_margin"
android:paddingTop="@dimen/footer_vertical_margin" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/track_button"
android:adjustViewBounds="true"
android:background="@android:color/transparent"
android:scaleType="centerInside" />
</RelativeLayout>
我是android应用程序的新手,非常感谢您的指导。总结一下我的评论:
删除不必要的
相对视图1
、2
和3
和滚动视图
。然后将底部图像按钮移动到线性布局上方,这样您就可以使用图像按钮上方的layout\u将线性布局定位,并将其高度设置为match\u parent
。这使得它填充顶部线性布局和底部图像按钮之间的空间。最后,使滚动视图的高度匹配\u parent
,这样它也将填充文本视图下方的剩余空间。总结我的评论:
删除不必要的相对视图1
、2
和3
和滚动视图
。然后将底部图像按钮移动到线性布局上方,这样您就可以使用图像按钮上方的layout\u将线性布局定位,并将其高度设置为match\u parent
。这使得它填充顶部线性布局和底部图像按钮之间的空间。最后,使滚动视图的高度与父视图匹配,这样它也将填充文本视图下方的剩余空间。您不能在滚动视图中使用列表视图。
您可以使用滚动视图
或列表视图
,但不能同时使用或在另一个视图中使用。您不能在滚动视图中使用列表视图。
您可以使用滚动视图
或列表视图
,但不能同时使用或一个在另一个中。我不会将列表视图放在滚动视图中。ListView已经可以滚动了。您的relativeLayout2和relativeLayout3是不必要的,因为它们每个只包装一个孩子。因为顶级元素是相对布局,所以可以指定无序的内容。在中间线性布局之前指定底部图像按钮。然后,只需将线性布局高度设置为与父级匹配,并将layout_设置为相对于relativeLayout1和底部图像按钮的上方和下方,这样它就会始终填充两者之间的剩余空间。将列表视图高度设置为与父项匹配实际上,您的relativeLayout1也没有必要,因为它还有一个子项。@Tenfour04我仔细地按照您的说明操作,现在它可以正常工作了。非常感谢,我对RelativeLayout、LinearLayout等有了清晰的理解。如果您能将您的评论作为答案发布,我可以接受。我不会将ListView放在ScrollView中。ListView已经可以滚动了。您的relativeLayout2和relativeLayout3是不必要的,因为它们每个只包装一个孩子。因为顶级元素是相对布局,所以可以指定无序的内容。在中间线性布局之前指定底部图像按钮。然后,只需将线性布局高度设置为与父级匹配,并将layout_设置为相对于relativeLayout1和底部图像按钮的上方和下方,这样它就会始终填充两者之间的剩余空间。将列表视图高度设置为与父项匹配实际上,您的relativeLayout1也没有必要,因为它还有一个子项。@Tenfour04我仔细地按照您的说明操作,现在它可以正常工作了。非常感谢您,我能够清楚地了解RelativeLayout、LinearLayout等。如果您能将您的评论作为答案发布,我可以接受。