2017-08-29 5 views
1

Ainsi, mon TextView personnalisé coupe la dernière ligne de texte dans certains cas. J'utilise une police personnalisée. Il semble seulement couper le dernier mot. J'ai changé mes vues de texte personnalisées en vues régulières, ce qui oblige mes vues de texte à utiliser des polices standard au lieu de celles personnalisées, et tout à coup le dimensionnement fonctionne.Police personnalisée provoquant la perte de ligne de mon ViewPager dynamique OnMeasure

Théorie au problème: En utilisant l'outil DDMS pour voir les tailles de mise en page autour de mes vues et en comparant la version de police personnalisée à la version de police normale, il semble bien mesurer la hauteur des caractères. couperait une partie des y, g, et p sur ma police plus grande et personnalisée. Mais alors il ne devine pas correctement la quantité de lignes que ma plus grande police va prendre, et mesure le nombre de lignes comme s'il s'agissait de la police plus petite, non personnalisée. Donc, quand une chaîne se termine par un mot de deux lettres qui est juste sur le bord de l'emballage, il ne pense pas qu'il doit s'enrouler à la ligne suivante et le coupe.

Mise à l'intérieur du ViewPager qui comprend l'enfant problème CustomTextView:

<LinearLayout 
    android:id="@+id/header_layout" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="-12dp" 
    android:paddingRight="-12dp" 
    android:orientation="vertical"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.CardView 
      snip> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <FrameLayout 
       snip> 
       <!---v--- this is the problem child---> 
       <com.snip.custom.views.CustomTextView 
        android:id="@+id/label" 
        style="@style/typographyParagraphBody" 
        android:layout_centerHorizontal="true" 
        android:gravity="center" 
        android:layout_marginBottom="20dp" 
        android:layout_marginLeft="30dp" 
        android:layout_marginRight="30dp" 
        android:text="@{carouselItemViewModel.label}"/> 

      </RelativeLayout> 
     </android.support.v7.widget.CardView> 
    </RelativeLayout> 
</LinearLayout> 

Boring ViewPager:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int height = 0; 
    for (int i = 0; i < getChildCount(); i++) { 
     View child = getChildAt(i); 
     child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
     int h = child.getMeasuredHeight(); 
     if (h > height){ 
      height = h; 
     } 
    } 

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

Mon fichier de mise en page:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@{carouselViewModel.carouselBackgroundColor}" 
    android:orientation="vertical"> 

    <com.snip.custom.views.CustomTextView 
     android:id="@+id/text" 
     style="@style/irrelevant" 
     android:text="@{carouselViewModel.text}"/> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tab_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="-17dp" 
     android:layout_gravity="top" 
     android:layout_below="@id/text" 
     app:tabBackground="@drawable/dot_tab_selector" 
     app:tabGravity="center" 
     app:tabIndicatorHeight="0dp" 
     app:tabPaddingEnd="5dp" 
     app:tabPaddingStart="5dp" 
     app:viewPager="@{{viewPager}}" /> 

    <com.snip.custom.views.CustomViewPager 
     android:id="@+id/view_pager" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clipToPadding="false" 
     android:paddingEnd="20dp" 
     android:paddingStart="20dp" 
     android:layout_below="@id/tab_layout" 
     app:currentItem="@{carouselViewModel.carouselCurrentItemSubject}" 
     app:setOffscreenPageLimit="@{carouselViewModel.carouselOffscreenPageLimitSubject}" 
     app:itemView="@{carouselViewModel.carouselItemViewSelector}" 
     app:items="@{carouselViewModel.carouselItems}" 
     app:onAdapterChangeListener="@{carouselViewModel.carouselAdapterChangeSubject}" 
     app:onPageChangeListener="@{carouselViewModel.carouselPageChangeSubject}"/> 

</RelativeLayout> 

Répondre

0

On dirait que je avais besoin plus de code pour mesurer correctement les enfants, puisqu'ils n'étaient pas conscients de leurs paramètres de disposition. Donc je les remesure en appliquant le parent widthMeasureSpec.

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int height = 0; 
    for (int i = 0; i < getChildCount(); i++) { 
     View child = getChildAt(i); 
     child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
     int h = child.getMeasuredHeight(); 
     if (h > height){ 
      height = h; 
     } 
     height = Math.max(child.getMeasuredHeight(), measureViewHeight(child, widthMeasureSpec)); 
    } 

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

private int measureViewHeight(View view, int widthMeasuredSpec) { 
    view.measure(getChildMeasureSpec(widthMeasuredSpec, 
      getPaddingLeft() + getPaddingRight(), 
      view.getLayoutParams().width), 
      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 
    return view.getMeasuredHeight(); 
}