Android Image is seen/not seen when orientation is

2019-09-05 17:34发布

问题:

I'm having some problems with ScrollView and an ImageView.

What I want is:

I have an ImageView set always at the top of the screen. Then, I have an ScrollView where go all elements of the View, and inside it I have another ImageView which it is set at the bottom of the screen when previous elements don't fill screen (usually Big Screens), and it is set at the bottom of the ScrollView when exists Scroll.

It is ok for some other Activities I have, but I have some problems in one Activity that has a FrameLayout.

What I get is:

(See code below) When I set in the RelativeLayout of the ImageView (the one to stay at the bottom) height = "wrap_content":

  • In portrait orientation: The Image don't have its real size because it doesn't fit on the screen. To fit in it, Scroll should appear. -> I don't know why it doesn't appear like in other Activities.
  • In landscape orientation: ImageView appears at the bottom of the screen. As other elements don't fit on the screen, Scroll appears inside FrameLayout.

When I set in the RelativeLayout of the ImageView (the one to stay at the bottom) height = "200dp" (real height of image is smaller):

  • In portrait orientation: The Image doesn't appear. There isn't any Scroll (it is not necessary because other elements are seen).
  • In landscape orientation: Appear two Scrolls. One inside FrameLayout for elements, and another for seeing the ImageView.

Here is my code up to date:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    tools:context=".FacturasActivity"
    android:orientation="vertical" >

    <LinearLayout 
        android:id="@+id/linearLayoutLogoFactor_factura"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <ImageView 
            android:id="@+id/imageViewLogoFactor_factura"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/logo_factorenergia"/>

    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollViewMain"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true" >

        <LinearLayout
            android:id="@+id/linearLayoutMain2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:layout_gravity="center_horizontal"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical" >

            <LinearLayout 
                android:id="@+id/linearLayoutDireccionFactura"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:layout_marginTop="10dp"
                android:weightSum="1.0">

                <TextView
                    android:id="@+id/textViewlabelDireccionFactura"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.3"
                    android:text="@string/etiqueta_direccion"
                    android:textSize="@dimen/textSize"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/textViewDireccion"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.7"
                    android:textSize="@dimen/textSize" />

            </LinearLayout>

            <FrameLayout
                android:id="@+id/frameLayoutFacturas"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dp"
                android:layout_marginRight="25dp"
                android:layout_marginTop="10dp" >

                <ListView
                    android:id="@android:id/list"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@android:id/empty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/facturas_vacia"
                    android:textSize="@dimen/textSize" />

            </FrameLayout>

            <RelativeLayout
                android:id="@+id/linearLayoutImagenInferior_main"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp" >

                <ImageView
                    android:id="@+id/imageViewImagenInferior_main"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:adjustViewBounds="true"
                    android:src="@drawable/la_electrica_de_las_empresas" />
            </RelativeLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

Can you help me? Thanks in advance.

回答1:

The solution I've found, looking at that question, is:

Create that function:

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        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();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
    }

and call it with my ListView. Only one Scroll (the one of ScrollView) is seen and used.