2016-04-05 9 views
1

最近StaticLayout.getHeight()メソッドをテストするカスタムビューを作成しましたが、実際には矛盾していることがわかりました。私が間違っているかを説明する前に、私は最初のコードを書き留めてみましょう:Androidのstaticlayoutの高さが一貫していないために奇妙なレンダリングのバグが発生する

これは私のMainActivity.java

package com.ayto.android.test; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.RelativeLayout; 

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     RelativeLayout mainParent = (RelativeLayout) findViewById(R.id.mainActivity); 
     RelativeLayout childRelativeLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.activity_test3,null); 
     noteLayout testNoteLayout = new noteLayout(this); 
     testNoteLayout.addView(childRelativeLayout); 
     mainParent.addView(testNoteLayout); 
    } 

} 

これは私のカスタム・レイアウトです:

package com.ayto.android.test; 
import android.content.Context; 
import android.graphics.RectF; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.RelativeLayout; 
public class noteLayout extends ViewGroup { 
    public noteLayout(Context activityContext) 
    { 
     super(activityContext); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     RelativeLayout mainParent = (RelativeLayout) getParent(); 
     for (int i = 0; i < getChildCount(); i++) { 
      RelativeLayout child = (RelativeLayout) getChildAt(i); 
      child.measure(MeasureSpec.makeMeasureSpec((mainParent.getWidth()/2), MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec((int) (child.getChildAt(0).getMeasuredHeight()), MeasureSpec.EXACTLY)); 
     } 
     setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     int numberOfChild = getChildCount(); 
     for (int i = 0;i<numberOfChild;i++){ 
      View childView = getChildAt(i); 
      float childHeight = (float) childView.getMeasuredHeight(); 
      float childWidth = (float) childView.getMeasuredWidth(); 
      RectF rect = new RectF(); 
      rect.bottom = childHeight; 
      rect.top = 20; 
      rect.left = 20; 
      rect.right = childWidth+20; 
      childView.layout((int) rect.left, (int) rect.top, (int) rect.right, (int) rect.bottom); 
     } 
    } 
} 

これは私のactivity_main.xmlです:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
    android:id="@+id/mainActivity"> 

    /> 
</RelativeLayout> 

これは私が膨張私activity_test3.xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#EF5350"> 

    <com.ayto.android.test.titleTextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/titleTextView" 
     android:textSize="18sp" 
     android:text="sadasdas das dasd asd as dasdasdasdsad sad asd asd asdasdasdsa sa zvczz xczxczxczx sadasasdsadsadsaasd " 

     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

私のカスタムレイアウトでは、自分自身を測定するrelativeLayoutの子を教えています。 relativeLayoutは、titleTextViewというカスタムビューを含むactivity_test3.xmlのrelativeLayoutです。このビューでは、onMeasure()メソッドをオーバーライドし、staticLayoutを使用して設定する高さを取得します。次に、titleTextViewの親であるrelativeLayoutは、基本的にそれ自身を測定し、titleTextViewである子の制約を受けます(説明するのが難しい混乱の原因を明確にするためのコードを参照してください)。

私の問題は、relativeLayoutの高さが必要なものよりも小さくなるため、子をクリップするということです。ここではいくつかのイメージされています

enter image description here

enter image description here

はまた、あなたが見ることができる、高さは両方の異なるデバイスのために実際に矛盾していると私はstaticLayout.getHeight()は一貫ピクセルを返すこととしていると考えサイズ。私はそれをどのように修正し、助けを望んでいるかわからない。

注:staticLayout.getHeightによるピクセルサイズの戻り値に密度を乗算しようとしましたが、大きすぎてその内部のテキストに完全に収まりません。

答えて

0

トップを追加し(あなたのケースでrect.top = 20)カスタムレイアウトonLayout方法でrect.bottomオフセット:高さの矛盾が異なる画素密度に起因する

protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     . 
     . 
     . 
     rect.bottom = childHeight + 20; 

デバイス。関連する情報はWhat is the difference between getWidth/Height() and getMeasuredWidth/Height() in Android SDK?

+0

にあります。回答ありがとうございます。それも私が思ったことです。この矛盾は、デバイス上のピクセル密度が異なるためですが、奇妙なことは、staticLayout.height()でピクセル戻り値に密度を掛けると、最初のイメージのデバイスがテキストにぴったり合っているということです。しかし、2番目のデバイスは非常に大きくなり、内部のテキストが必要以上になります。私は、私が密度でそれを乗算することについて上で言及したと思う。 – Wowzer

関連する問題