2

Appbarlayout hat eine TextView kann eine große Anzahl von Text enthalten.Android Appbarlayout mit einem großen Text Höhe Ausgabe

Beim Start der TextView 2 Zeilen und alles ist in Ordnung, aber wenn ich die TextView die Appbarlayout erweitern wird geschnitten und als ich den Rest nach oben ist leer.

Normal: normal

Expanded: expanded

nach oben gescrollt: scrolled up

Layout-Code:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/view_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

    <android.support.design.widget.CoordinatorLayout 
     android:id="@+id/coordinator" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/black" 
      app:elevation="0dp" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/holo_green_dark" 
       app:layout_scrollFlags="scroll"> 

       <TextView 
        android:id="@+id/text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="200dp" 
        android:maxLines="2" 
        android:text="test" /> 

      </LinearLayout> 

      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="50dp" 
       android:background="@android:color/holo_blue_bright" 
       android:text="TAB" /> 

     </android.support.design.widget.AppBarLayout> 

     <android.support.v4.widget.NestedScrollView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@android:color/holo_orange_light" 
      app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/pager" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" /> 
     </android.support.v4.widget.NestedScrollView> 
    </android.support.design.widget.CoordinatorLayout> 
</LinearLayout> 

Aktivitätscode:

package me.ffts.demo; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    boolean isOpen = false; 
    TextView text; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     text = (TextView) findViewById(R.id.text); 
     text.setText("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); 
     text.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (isOpen) { 
        text.setMaxLines(2); 
        isOpen = false; 
       } else { 
        text.setMaxLines(Integer.MAX_VALUE); 
        isOpen = true; 
       } 
      } 
     }); 
    } 
} 

Ist das ein Fehler von AppbarLayout oder CoordinatorLayout?

+0

warum hast Du android: layout_marginTop = „200dp“ und android: maxLines = „2“ Bitte entfernen Sie diese und versuchen Sie dann –

+0

In meiner App gibt es einige Inhalte über dem Textview, so dass ich eine MarginTop zu den Fall simulieren. Und die TextView kann expandieren oder schließen, wenn ich darauf klicke, um den Detailtext ein- oder auszublenden, also ändere ich die MaxLines von TextView, um das zu tun. Ich entfernte die MaxLines und TextView beim Start wie der zweite Screenshot erweitert, und wenn ich scrollte, zeigte es immer noch die leere wie der dritte Screenshot. – ffts

+0

Testen Sie in meinem Telefon und Emulator und habe nicht das Problem, das Sie sagen. Vielleicht der Fehler deines Telefons? –

Antwort

0

Es sieht so aus, als ob das AppBarLayout seine Höhe abschneidet; Wenn Sie das android:layout_height hart zu etwas massivem wie 2000 dp codieren, werden Sie sehen, dass es alles zeichnet. Ich habe das Problem behoben, indem ich das AppBarLayout gezwungen habe, alle seine untergeordneten Elemente zu messen und dann seine Höhe festzulegen.

public class HeightWrappingAppBarLayout extends AppBarLayout { 

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

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int height = 0; 
     for (int i = 0; i < getChildCount(); i++) { 
      View child = getChildAt(i); 
      if (child.getVisibility() != GONE) { 
       measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); 
       height += child.getMeasuredHeight(); 
      } 
     } 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
     setMeasuredDimension(width, height); 
    } 
} 
0

Verwenden Sie MeasureSpec.UNSPECIFIED für Höhe.

public class ScrollAppBarLayout extends AppBarLayout { 
    public ScrollAppBarLayout(Context context) { 
     super(context); 
    } 

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

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, 
       MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.UNSPECIFIED)); 
    } 

    @Override 
    protected void measureChild(View child, int parentWidthMeasureSpec, 
           int parentHeightMeasureSpec) { 
     ViewGroup.LayoutParams lp = child.getLayoutParams(); 

     int childWidthMeasureSpec; 
     int childHeightMeasureSpec; 

     childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, getPaddingLeft() 
       + getPaddingRight(), lp.width); 

     childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
       MeasureSpec.getSize(parentHeightMeasureSpec), MeasureSpec.UNSPECIFIED); 

     child.measure(childWidthMeasureSpec, childHeightMeasureSpec); 
    } 

    @Override 
    protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, 
              int parentHeightMeasureSpec, int heightUsed) { 
     final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); 

     final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, 
       getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin 
         + widthUsed, lp.width); 
     final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
       MeasureSpec.getSize(parentHeightMeasureSpec), MeasureSpec.UNSPECIFIED); 

     child.measure(childWidthMeasureSpec, childHeightMeasureSpec); 
    } 
}