Вопрос

I would like to position a ScrollView at a certain part of screen independently of the screen size and resolution. E.g. I would like it to take up the bottom 30% of the space. Above this ScrollView I want to have an image and empty space. I have tried that by putting all the views into a linear layout and assigning weights, but that doesn't seem to work. Any ideas about the solution?

The layout I have at the moment:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

    <ImageView
        android:id="@+id/appLogo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/logo"/>

    <ProgressBar 
        android:id="@+id/progressbar"
        android:indeterminate="true"
        android:layout_width="50dp" 
        android:layout_height="50dp"
        style="?android:attr/progressBarStyle"
        android:indeterminateBehavior="cycle"
        android:layout_below="@id/appLogo"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_vertical|center_horizontal"/>

    <ScrollView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/appLogo">

        <LinearLayout 
            android:id="@+id/startLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            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">

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Inside the ScrollView I have a LinearLayout, which I use to put in TextView and ImageView elements programaticaly. What I want to achieve is that no no matter the screen size, there would always be visible a maximum of 3 child elements.

What I tried to do:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">" 

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/appLogo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_weight="70"
            android:src="@drawable/logo"/>

        <ProgressBar 
            android:id="@+id/progressbar"
            android:indeterminate="true"
            android:layout_width="50dp" 
            android:layout_height="50dp"
            style="?android:attr/progressBarStyle"
            android:indeterminateBehavior="cycle"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_vertical|center_horizontal"/>

        <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="30">

            <LinearLayout 
                android:id="@+id/startLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                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">

            </LinearLayout>
        </ScrollView>

    </LinearLayout>
</RelativeLayout>

But the ScrollView only end ups taking even more space and the image on top gets smaller.

Это было полезно?

Решение

I added a linearlayout for imageview and progress bar and gave it 0dp height and 70 weight.

Also notice that parent linearlayout has a weightsum set to 100.

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" android:weightSum="100">
  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="70"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/appLogo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@drawable/logo"/>

        <ProgressBar 
            android:id="@+id/progressbar"
            android:indeterminate="true"
            android:layout_width="50dp" 
            android:layout_height="50dp"
            style="?android:attr/progressBarStyle"
            android:indeterminateBehavior="cycle"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_vertical|center_horizontal"/>
        </LinearLayout>

        <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="30">

            <LinearLayout 
                android:id="@+id/startLayout"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                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">

            </LinearLayout>
        </ScrollView>

    </LinearLayout>
</RelativeLayout>

Другие советы

// try this and let me know if any problem i'm done your requirement with linear layout..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <ImageView
        android:id="@+id/appLogo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_weight="0.70">
    <ProgressBar
        android:id="@+id/progressbar"
        android:indeterminate="true"
        android:layout_width="50dp"
        android:layout_height="50dp"
        style="?android:attr/progressBarStyle"
        android:indeterminateBehavior="cycle"/>
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.30">

        <LinearLayout
            android:id="@+id/startLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"/>
    </ScrollView>
</LinearLayout>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top