5

我有一个包含 14 个字段的自定义列表视图。像这样:

字段1 字段2 字段3 字段4 字段5 字段6 字段7 字段8 字段9 字段10 字段11 字段12 字段13 字段14。现在明显的问题是,我不可能将所有字段都放在屏幕上,所以我想让整个列表视图水平滚动,所以我只能显示 5-6 个字段,当用户水平滚动时,他将能够看到他们的其余部分。我可以有一个垂直和水平滚动的列表视图吗?

4

4 回答 4

10

只需在水平滚动视图内创建列表视图,骨架如下

<HorizontalScrollView ...........>
    <LinearLayout ......>
        <LinearLayout ......>
        //List View Titles will be here
        </LinearLayout>

        <ListView ........ android:layout_weight="1" />

    </LinearLayout>
</HorizontalScrollView>

在这里,如果您需要在列表视图中显示不可滚动的标题,然后如上所述将其添加到单独的布局中,并且不要忘记设置layout_weight属性。

于 2012-12-04T09:38:32.693 回答
0

您应该有一个 Horizo​​ntalScrollView,它应该是该链接中给出的 VerticalScrollView 的子级。

于 2012-12-04T09:28:15.123 回答
0

实现水平列表视图的最简单方法。

水平滚动.xml

 <?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadeScrollbars="false"
android:background="@color/list_item_bg_color">

 </HorizontalScrollView>

水平列表.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/horizontal_outer_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:background="@drawable/rect_border_box"
android:orientation="vertical" >



    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
   </Linearlayout>

爪哇代码:

      LinearLayout parent = (LinearLayout) getLayoutInflater().inflate(
            R.layout.observation_detail_parent_layout, null);
      Linearlayout child= null;


     //iterate views upto you adapater count.
      for(int i=0;i<14;i++){
      child = (LinearLayout) getLayoutInflater().inflate(
                    R.layout.horizontal_list, null);

        //set your views specified in horizontal_list.xml

      TextView text = (TextView) findViewById(R.id.text);
      text.setText("your text");
      parent.addView(child);
    }
于 2012-12-04T10:13:19.123 回答
0

列表视图已经支持垂直滚动,所以我们需要支持水平滚动。下面的代码对我来说工作得很好。请注意,即使“权重为 1”,也不要为列表视图保留“高度为 0dp”。因为 Horizo​​ntalScrollView 会将高度设为零,并且不会为列表视图绘制任何项目。

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/table_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TextView
            android:layout_width="58dp"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:text="header"
            android:textColor="@android:color/black" />

        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ListView
                android:id="@+id/mylist"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1" >
            </ListView>
        </HorizontalScrollView>
    </LinearLayout>
于 2015-09-17T19:36:07.700 回答