Ich habe eine Activity
mit zwei Fragment
s (eine Liste eine normale). Die Fragment
bläst eine Scrollview
auf, die eine LineaLayout
(vertikal) enthält, und dieses Layout enthält TextViews
. Die ScrollView
und layout_width
und layout_height
sind match_parent
. Aber unten gibt es immer noch eine "Lücke" ... Ich hoffe, Sie können mir helfen.
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>
das Fragment, das dieses Layout aufbläst.
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 };
}
Danke vielmals.
Edit: Auch das Problem ist schon behoben mit: Android: fillViewPort = "true", ich möchte dir das Problem zeigen.
Aber ich habe nicht genug Ruf, um ein Bild zu posten ... Entschuldigung!
Wenn ich mich nicht irre, wird die Höhe von ViewGroup
(in Ihrem Fall LinearLayout
), dh das (einzige) Kind in einem ScrollView
, immer als wrap_content interpretiert, da dieser Inhalt größer sein kann als die Höhe von ScrollView
.
Dies bedeutet auch, dass, wenn der Inhalt kleiner ist, der Inhalt (Kind) von ScrollView
nicht unbedingt den Bildschirm ausdehnen muss.
Um Ihnen bei der Behebung dieses Problems zu helfen, benötigen Sie einen Screenshot Ihres Problems.
Wenn Sie Android:fillViewport="true"
für ScrollView
festlegen, wird das Problem möglicherweise behoben:
<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">
<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" >
Fügen Sie in Ihrer ScrollView
ein Attribut hinzu.
Android:fillViewport="true"
inflater.inflate(R.layout.menu_content_main, container);
sollte sein
inflater.inflate(R.layout.menu_content_main, container, false);
Ich hatte ein ähnliches Problem und konnte es nur mit einer Hilfsklasse beheben. Ich habe den Originalcode online gefunden, und dies ist meine Implementierung.
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);
}
}
<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" />