0

I have this ImageView I want cropped to occupy no more than 1/3 the screen size. I have used the following layout using layout_height="0dp" and layout_weight where needed but the ImageView keeps taking more height than it should. If I replace the ScrollView with LinearLayout everything falls into place as it should. My guess is this is because the ScrollView has unlimited height... How can I still force the ScrollView to honor the real layout height of its parent and achieve what I want.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
>
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true"
    >
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
        >
            <ImageView
                android:id="@+id/imageview"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:src="@drawable/image"
                android:scaleType="centerCrop"
            />
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="2"
                android:orientation="vertical"
            >
                <TextView
                    android:id="@+id/textview1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:layout_marginLeft="16dp"
                    android:textAppearance="@style/TextAppearance.AppCompat.Headline"
                    android:text="Text1"
                />
                <TextView
                    android:id="@+id/textview2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:text="Text2"
                />
                <TextView
                    android:id="@+id/textview3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="4dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="16dp"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                    android:singleLine="true"
                    android:ellipsize="end"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:text="Text3"
                />
                <TextView
                   android:id="@+id/textview4"
                   android:layout_width="fill_parent"
                   android:layout_height="wrap_content"
                   android:layout_marginTop="4dp"
                   android:layout_marginBottom="4dp"
                   android:layout_marginLeft="16dp"
                   android:layout_marginRight="16dp"
                   android:textAppearance="@style/TextAppearance.AppCompat.Body2"
                   android:singleLine="false"
                   android:text="Text4"
                />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

Here's how it looks like:

Layout Final

smichak
  • 4,716
  • 3
  • 35
  • 47

2 Answers2

1

A Scrollview has "infinite height" according to the docs:

http://developer.android.com/reference/android/widget/ScrollView.html

So, "1/3" height really has no meaning - the LinearLayout will simply occupy whatever height it needs/wants/can within a Scrollview.

if you want a View to be 1/3 of the screen size, but you also want it to scroll, then use DisplayMetrics to calculate the height of your screen and then layout elements accordingly. (Likewise, if you want the "top" portion to be 2/3 of the screen height, do the same.)

See this post:

How to get screen display metrics in application class

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
0

Inside your ImageView, try this

android:layout_gravity="center_horizontal"
android:scaleType="centerInside"
Abhinav Arora
  • 537
  • 1
  • 7
  • 23