简体   繁体   中英

Horizontal Scrollview containing a LinearLayout with a Listview

I have LinearLayout which hierarchy looks like this: HorizontalScrollview as root, it contains a vertical LinearLayout and inside of it a horizontal LinearLayout as the header (made with TextViews ) and a ListView just below.

Basically, the idea is to provide an horizontal scroll (since the ListView already has its vertical scroll). I almost got it, the problem is that the ListView don't expand its width enough to fit the content.

Here is the code of the Layout:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/hscrollMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="170dp"
                android:layout_height="wrap_content"
                android:text="@string/fecha"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvFechaDet"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="@string/tipo"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tvTipoEvtDet"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/descripcion"
                android:textStyle="bold" />
        </LinearLayout>

        <ProgressBar
            android:id="@+id/pbEventListing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:indeterminate="true"
            android:visibility="gone" />

        <ListView
            android:id="@+id/lvEventos"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>
</HorizontalScrollView>

I'm using a custom ArrayAdapter with a custom layout for the list item, the code is below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tvFechaEvtItem"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:text="@string/fecha" />

    <ImageView
        android:id="@+id/ivEstadoEvtItem"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:contentDescription="@string/estado"
        android:scaleType="fitXY"
        android:src="@drawable/ic_warning" />

    <TextView
        android:id="@+id/tvTipoEvtItem"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:text="@string/tipo" />

    <TextView
        android:id="@+id/tvDescEvtItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:text="@string/descripcion" />

</LinearLayout>

PD: Yeah I succesfully formatted the code with the SO editor, the last time I messed it up xD.

It might be that you have set the first LinearLayout width to be wrap content. Try changing it to match_parent

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/hscrollMain"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM