15

Ich habe ein Layout erstellt mit einer ScrollView, die eine PercentRelativeLayout als Kind hat. Es funktioniert nicht auf Lollipop und älteren Geräten, funktioniert aber auf Marshmallow-Geräten. Bitte überprüfen Sie den Code unten:PercentRelativeLayout in ScrollView

<ScrollView 
    android:id="@+id/scrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true"> 

    <android.support.percent.PercentRelativeLayout 
     android:id="@+id/scrollContent" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_heightPercent="100%" 
     app:layout_widthPercent="50%"> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_red_dark" 
      android:text="kkjknadko" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textView1" 
      android:text="Abcaad" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview2" 
      android:background="@android:color/holo_red_dark" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview3" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview4" 
      android:background="@android:color/holo_red_dark" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview5" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview6" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

     <TextView 
      android:id="@+id/textview8" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/textview7" 
      android:text="Abcd" 
      android:textColor="@android:color/black" 
      app:layout_heightPercent="10%" 
      app:layout_marginTopPercent="10%" 
      app:layout_widthPercent="50%"/> 

    </android.support.percent.PercentRelativeLayout> 
</ScrollView> 

Und auch android:fillViewport="true" ich, es zeigt nichts in Lollipop und älteren Android-Versionen.

Leider funktioniert das Prozent Layout nicht mit ScrollView vor M. Der Grund dafür ist, dass sie von der Größenhinweis abhängen, der im Messschritt geliefert wird. Vor M würden die meisten Layouts den Größenhinweis 0 liefern, wenn sie eine nicht spezifizierte Messspezifikation senden.

können Sie versuchen, das zu beheben, indem Sie Ihre eigene Unterklasse von ScrollView und überwiegenden measureChild und measureChildWithMargins zu schaffen (zum Glück beide geschützt sind), um die Größe Hinweis zu liefern.

Quelle - plus.google.com.

Kann mir jemand bei der Erstellung von benutzerdefinierten ScrollView helfen, damit es funktioniert?

+0

Try NestScrollView statt Scrollview zu verwenden. –

+0

Ich habe das versucht, aber es funktioniert nicht –

Antwort

0

Die Frage, wie ich es verstehe, ist: Ich möchte diese TextViews 50% Breite der übergeordneten Ansicht sein.

Weg, der funktioniert für mich ist:

<Space 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:id="@+id/space" /> 

dann die Ansicht (n) zu, dass allign. Entweder das RelativeLayout oder die TextViews. Sie verlieren die Kontrolle (wenn Sie jemals hatten) über den Prozentsatz der RelativeLayout. Ich neige dazu, Dinge wie zu gehen:

android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 

Hilft das?

+0

Nein, Eigentlich muss ich percentRelativeLayout in scrollview arbeiten. Ich habe oben nur ein Beispiel gegeben, um das Szenario nicht genau das selbe zu zeigen, das ich benutze –

7

Erstellen Sie eine benutzerdefinierte scrollview und setzen Measured Height und Width wie Sie es für Beispiel wollen

CustomScrollView

public class CustomScrollView extends ScrollView { 


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

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

    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     int size = 0; 
     int width = getMeasuredWidth(); 
     int height = getMeasuredHeight(); 

     if (width > height) { 
      size = height; 
     } else { 
      size = width; 
     } 
     setMeasuredDimension(size, size); 
    } 
} 

Verwenden customscrollview in Ihrem xml.

<yourscrollviewclasspath.CustomScrollView   // here you have scrollview path like com.yourpackage.folder_where_your_scrollview_lies 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="true" 
     > 
</yourscrollviewclasspath.CustomScrollView > 

enter image description here Hoffnung, das wird helfen.

0

traf ich das gleiche Problem mit Android-K und android L.

Sie Constraint Layout statt Prozent relatives Layout verwendet haben könnten, aber es scheint auch mit dem Problem zu scrollen. Selbst die benutzerdefinierte Scroll-Ansicht, wie in anderen Antworten vorgeschlagen, war für mich nicht hilfreich.

Obwohl es einen Weg gibt.

Konvertieren Sie Ihr prozentuales relatives Layout in das relative Layout und legen Sie die Höhe der Ansichten programmgesteuert entsprechend Ihrer Gerätehöhe fest.

Es scheint kein Problem beim Scrollen mit relativem Layout. Und Sie können die Space-Ansicht für den Abstand zwischen TextViews verwenden. Dies ist keine sehr elegante Lösung, aber für mich hat es funktioniert.

Java-Code:

int height = getWindowManager().getDefaultDisplay().getHeight(); 
int width = getWindowManager().getDefaultDisplay().getWidth(); 

// to set height to each textview to 10% of screen 
textView1.getLayoutParams().height = (int) (height * 0.10); 
textView2.getLayoutParams().height = (int) (height * 0.10); 
textView3.getLayoutParams().height = (int) (height * 0.10); 
textView4.getLayoutParams().height = (int) (height * 0.10); 
textView5.getLayoutParams().height = (int) (height * 0.10); 
textView6.getLayoutParams().height = (int) (height * 0.10); 

// to set width to each textview to 50% of screen 
textView1.getLayoutParams().width = (int) (width * 0.50); 
textView2.getLayoutParams().width = (int) (width * 0.50); 
textView3.getLayoutParams().width = (int) (width * 0.50); 
textView4.getLayoutParams().width = (int) (width * 0.50); 
textView5.getLayoutParams().width = (int) (width * 0.50); 
textView6.getLayoutParams().width = (int) (width * 0.50); 

// for margin between textviews 
space1.getLayoutParams().height = (int) (height * 0.10); 
space2.getLayoutParams().height = (int) (height * 0.10); 
space3.getLayoutParams().height = (int) (height * 0.10); 
space4.getLayoutParams().height = (int) (height * 0.10); 
space5.getLayoutParams().height = (int) (height * 0.10); 

XML-Code:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/scrollView" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true"> 

<RelativeLayout 
    android:id="@+id/scrollContent" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@android:color/holo_red_dark" 
     android:text="kkjknadko" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space1" 
     android:layout_below="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space1" 
     android:text="Abcaad" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space2" 
     android:layout_below="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space2" 
     android:background="@android:color/holo_red_dark" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space3" 
     android:layout_below="@+id/textView3" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space3" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space4" 
     android:layout_below="@+id/textView4" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textview5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space4" 
     android:background="@android:color/holo_red_dark" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

    <android.support.v4.widget.Space 
     android:id="@+id/space5" 
     android:layout_below="@+id/textview5" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" /> 

    <TextView 
     android:id="@+id/textview6" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/space5" 
     android:text="Abcd" 
     android:textColor="@android:color/black"/> 

</RelativeLayout>