4

我正在尝试将 aListView放在ScrollViewandroid 中。我试着把它们放在这样的LineaLayout里面

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/frameLayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical" >
  <ScrollView 
      android:id="@+id/marketDetailScrollView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
   ...
  </ScrollView>
  <ListView  android:id="@android:id/list" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:background="#FFF"/>
</LinearLayout>

并且ListView没有显示。我也尝试将其放入 a 中RelaviteLayout,但仍然没有。我可以以某种方式拥有一个ListViewunder aScrollView吗?

只是为了添加一些东西。我不想分割我的屏幕,让我有一半ScrollView和另一半ListView。我希望用户向下滚动ScrollView显然大于屏幕尺寸然后ListView应该开始

4

5 回答 5

10

我建议您将 ScrollView 内容作为 HeaderView 放在 ListView 中,或者您是否明确希望在屏幕上有两个单独的可滚动区域?

将滚动视图的内容作为标题(单个可滚动区域)放在列表​​视图中的示例:

public void onCreate(Bundle s){
    setContentView(R.id.yourLayout);

    ListView listView = (ListView) findViewById(R.id.listView);

    // Set the adapter before the header, because older 
    // Android version may have problems if not

    listView.setAdapter(new YourAdapter());

    // Add the header
    View header = inflater.inflate(
            R.layout.you_layout_that_was_in_scrollview_before, null, false); 

    listView.addHeaderView(header);

}

活动的布局如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView  android:id="@+id/listView" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"
    android:background="#FFF"/>
</LinearLayout>

如果你想要两个可滚动的区域,你应该使用布局权重:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView 
  android:id="@+id/marketDetailScrollView"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1" >
  <!-- ScrollViewContent -->
</ScrollView>
<ListView  android:id="@android:id/list" 
android:layout_height="0dp" 
android:layout_width="match_parent"
android:background="#FFF"
android:layout_weight="1"/>
</LinearLayout>
于 2013-08-20T10:08:45.757 回答
0

Fill parent对您不起作用,将其更改为包装内容,并将 layout_weight 1 也用于滚动视图,然后它可能对您有用

于 2013-08-20T09:47:19.900 回答
0

通常将listview放在scrollview中是不好的。但是如果需要的话,你需要设置listview的高度。按照下面的链接计算列表视图的高度以适合滚动视图。

http://www.jiramot.info/android-listview-in-scrollview-problem

使用这种方式,您可以实现您想要的

于 2013-08-20T09:49:43.737 回答
0

您必须将滚动视图及其元素的内容高度设置为“包装内容”。XML 将是这样的:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView 
  android:id="@+id/marketDetailScrollView"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" >
...(height will be wrap_content for inner element as well)
</ScrollView>
<ListView  android:id="@android:id/list" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent"
android:background="#FFF"
android:layout_weight="1"/>
</LinearLayout>

这可能会帮助你..

于 2013-08-20T09:51:42.910 回答
0

我认为这与您正在寻找的东西很接近。我给你这段代码,但我必须告诉你,把 aListView放在 aScrollView是一种不好的做法,你不应该使用这个解决方案。你应该做一些更正确的事情,并且在滚动视图中没有滚动视图。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:android1="http://schemas.android.com/apk/res/android"
    android:id="@+id/containerScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/scrollViewContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ScrollView
            android:id="@+id/marketDetailScrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/scrollViewContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <!-- the views contained in your ScrollView goes here -->
            </LinearLayout>
        </ScrollView>

        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFF" />
    </LinearLayout>

</ScrollView>
于 2013-08-20T09:51:47.690 回答