web-dev-qa-db-fra.com

ScrollView Layout ne remplit pas tout l'écran

J'ai un Activity avec deux Fragments (un liste un normal). Et le Fragment normal gonfle un Scrollview contenant un LineaLayout (vertical) et cette présentation contient TextViews. Les ScrollView et layout_width et layout_height sont match_parent; Mais au fond, il y a encore un "écart" ..___ J'espère que vous pourrez m'aider.

ScrollView.xml

<ScrollView
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
Android:id="@+id/scrollView1"
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"
    Android:background="@color/titlescreen_bg"
    Android:orientation="vertical"
    Android:paddingTop="60dp"
    tools:context=".MainActivity" >

    <TextView
        Android:id="@+id/tv_headline"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:gravity="center"
        Android:paddingBottom="60dp"
        Android:paddingTop="60dp"
        Android:textIsSelectable="false"
        Android:textSize="@dimen/fontsize_slogan_titlescreen" />

    <TextView
        Android:id="@+id/tv_content"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:gravity="center"
        Android:paddingBottom="30dp"
        Android:paddingTop="30dp"
        Android:textIsSelectable="false"
        Android:textSize="@dimen/fontsize_slogan_titlescreen" />
</LinearLayout>

</ScrollView>

le fragment gonflant cette mise en page.

package wak.iage.layout;

import wak.iage.R;
import Android.app.Fragment;
import Android.graphics.Color;
import Android.graphics.Typeface;
import Android.os.Bundle;
import Android.view.Gravity;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;
import Android.view.ViewGroup.LayoutParams;
import Android.widget.LinearLayout;
import Android.widget.TextView;

public class MenuContentFragment extends Fragment
{
LinearLayout.LayoutParams   relativeParams  = new LinearLayout.LayoutParams(
                                                    LayoutParams.MATCH_PARENT,
                                                    LayoutParams.MATCH_PARENT);
LinearLayout                topLayout       = null;
TextView                    body            = null;
TextView                    head            = null;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.menu_content_main, container);
    return v;
}

public void changeText(String title, String content) {
    topLayout = (LinearLayout) getActivity().findViewById(
            R.id.LinearLayout1);
    head = (TextView) getActivity().findViewById(R.id.tv_headline);
    body = (TextView) getActivity().findViewById(R.id.tv_content);

    if (body == null) {
        topLayout.removeViews(1, topLayout.getChildCount() - 1);
        body = new TextView(getActivity().getApplicationContext());
        body.setPadding(0, 30, 0, 20);
        body.setTextColor(Color.BLACK);
        body.setTextSize(22);
        body.setGravity(Gravity.CENTER_HORIZONTAL);
        topLayout.addView(body, relativeParams);
    }

    body.setText(content);
    head.setText(title);
}

public void addGlossary() {
    if (body != null) {
        topLayout.removeView(body);
    }

    int i = 0;

    for (int id : GLOSSARY) {
        TextView glossary = new TextView(getActivity()
                .getApplicationContext());
        glossary.setText(getString(id));
        glossary.setTextColor(Color.BLACK);
        if (i % 2 == 0) {
            glossary.setTypeface(Typeface.DEFAULT_BOLD);
            glossary.setTextSize(22);
            glossary.setPadding(0, 10, 0, 10);
        }
        topLayout.addView(glossary, relativeParams);
        i += 1;
    }
}

public static final int[]   GLOSSARY    = {
        R.string.GlossaryAndroidOSTitle, R.string.GlossaryAndroidOSContent,
        R.string.GlossaryAppTitle, R.string.GlossaryAppContent,
        R.string.GlossaryCloudTitle, R.string.GlossaryCloudContent,
        R.string.GlossaryDonwloadTitle, R.string.GlossaryDonwloadContent,
        R.string.GlossaryFacebookTitle, R.string.GlossaryFacebookContent,
        R.string.GlossaryGPSTitle, R.string.GlossaryGPSContent,
        R.string.GlossaryHomescreenTitle,
        R.string.GlossaryHomescreenContent, R.string.GlossaryPasswordTitle,
        R.string.GlossaryPasswordContent, R.string.GlossaryRouterTitle,
        R.string.GlossaryRouterContent, R.string.GlossarySDTitle,
        R.string.GlossaySDContent, R.string.GlossayStandbyTitle,
        R.string.GlossayStandbyContent, R.string.GlossaryTabletTitle,
        R.string.GlossaryTabletContent, R.string.GlossaryTouchscreenTitle,
        R.string.GlossaryTouchscreenContent, R.string.GlossayWidgetsTitle,
        R.string.GlossayWidgetsContent, R.string.GlossayWLANTitle,
        R.string.GlossayWLANContent };
}

Merci beaucoup.

Edit: Même le problème est déjà résolu avec: Android: fillViewPort = "true", je veux vous montrer le problème.

Mais je n'ai pas assez de réputation pour poster une image ... désolé!

61
j0chn

Si je ne me trompe pas, la hauteur de ViewGroup (la hauteur de LinearLayout dans votre cas), qui est le (seul) enfant à l'intérieur d'un ScrollView, est toujours interprétée comme un contenu wrap_content, car ce contenu peut être plus grand que la hauteur de ScrollView (d'où les barres de défilement) .

Cela signifie également que si le contenu est plus petit , le contenu (enfant) de ScrollView (enfant) ne peut pas nécessairement s'étirer pour remplir l'écran. 

Afin de vous aider visuellement à résoudre ce problème, nous devons voir une capture d'écran de votre problème.

Peut-être que régler Android:fillViewport="true" sur ScrollView résoudra votre problème:

<ScrollView
    xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:id="@+id/scrollView1"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:fillViewport="true">
220
Streets Of Boston
<ScrollView xmlns:Android="http://schemas.Android.com/apk/res/Android"
    Android:layout_width="fill_parent"
    Android:layout_height="fill_parent"
    Android:fillViewport="true"
    Android:fadeScrollbars="false"
    Android:scrollbars="vertical" >

Dans votre ScrollView ajouter un attribut c'est-à-dire.

Android:fillViewport="true"
7
Trojan Horse
inflater.inflate(R.layout.menu_content_main, container);

devrait être

inflater.inflate(R.layout.menu_content_main, container, false);
1
alex

J'ai eu un problème similaire et ne pouvais le résoudre qu'avec une classe Helper. J'ai trouvé le code original en ligne et ceci est mon implémentation.

Classe Java:

public class ImageViewHelper extends Android.support.v7.widget.AppCompatImageView {

    public ImageViewHelper(Context context) {
        super(context);
    }

    public ImageViewHelper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageViewHelper(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Drawable d = getDrawable();
        if (d != null) {
            int w = MeasureSpec.getSize(widthMeasureSpec);
            int h = w * d.getIntrinsicHeight() / d.getIntrinsicWidth();
            setMeasuredDimension(w, h);
        }
        else super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

XML:

<com.example.app.ImageViewHelper
    Android:id="@+id/img"
    Android:src="@drawable/start"
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:layout_alignParentTop="true"
    Android:layout_centerHorizontal="true"
    Android:adjustViewBounds="true" />
0
Dan Oprean