android 滚动视图整个 activity 具有列表视图

android scroll view on whole activity having listview

我想要 android activity 的特定要求。创建一个论坛 activity,标题 post,评论列表和评论按钮。

要求是:

  1. 标题在最上面
  2. post 低于
  3. post编辑此 post
  4. 的用户名
  5. 评论列表视图
  6. 评论按钮始终显示在 activity 布局的底部。
  7. 以上定义为滚动视图的所有内容

我在这里面临的问题是当标题的高度和 post 增加了列表视图并且按钮离开了 android 视图。当我试图将滚动视图放在整个 activity 按钮出现在列表视图的正下方,并不总是在底部。

P.S 我是 android 的初学者。

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.trippals.android.SinglePostView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/clubtitle"
android:background="@drawable/abc_list_selector_background_transition_holo_ligh    t"
    android:textSize="25sp"
    android:textStyle="bold"
  />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/data"
    android:layout_below="@+id/clubtitle"
    android:layout_marginTop="15dp"
    android:background="@drawable/abc_list_selector_background_transition_holo_light"
    android:textSize="20sp"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/user"
    android:textStyle="bold"
    android:layout_below="@+id/data"
    android:layout_marginTop="20dp"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/user"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:weightSum="2"
    >

<ListView
    android:id="@+id/commentList"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.8"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:divider="@android:color/transparent"
    android:layout_marginTop="5dp"
    android:dividerHeight="10.0sp"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight=".2"
    android:orientation="horizontal"
    android:weightSum="1"
    >
<EditText
    android:id="@+id/commentbox"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="comment"
    android:inputType="text"
    android:layout_weight=".9"
    />
 <ImageButton
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@drawable/ic_play_dark"
        android:id="@+id/addbutton"
     android:layout_weight=".1"
 />
</LinearLayout>
</LinearLayout>

<ProgressBar
    android:id="@+id/pbLoadingpost"
    android:visibility="invisible"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_centerInParent="true"
    android:layout_gravity="center"
    />

要在滚动视图中使用列表视图,您需要在 运行 时间以编程方式为列表视图提供固定高度,即项目数 * 项目高度, 因为 ScrollView 需要知道其中每个视图的高度。

使用此代码获取列表视图的高度。

int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(0, 0);
    totalHeight += listItem.getMeasuredHeight();
}

我遇到过同样的问题。但是当我学习 udacity android 设计教程时,我学会了如何根据您的要求确定要使用的布局。有一个视频教如何识别布局。我保证它会对你有很大帮助。 我对你的问题的解决方案是: 您将要始终显示在 activity 底部的按钮保留在滚动视图之外,并保持滚动视图的高度与按钮高度所需的边距相匹配。

看看这个页面: http://blog.lovelyhq.com/setting-listview-height-depending-on-the-items/

正如 Ashim 所说,如果你想在 ScrollView 中使用 ListView,你必须根据项目的数量设置 ListView 的高度(当它是 ScrollView 的子级时,你不应该有一个滚动项目的 ListView应该处理滚动)。