Java Android静态布局高度不一致,导致奇怪的渲染错误

Java Android静态布局高度不一致,导致奇怪的渲染错误,java,android,Java,Android,我最近创建了一个自定义视图来测试StaticLayout.getHeight()方法,我发现它确实不一致。在解释错误之前,让我先写下代码: 这是我的MainActivity.java package com.ayto.android.test; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.RelativeLayout; public cl

我最近创建了一个自定义视图来测试StaticLayout.getHeight()方法,我发现它确实不一致。在解释错误之前,让我先写下代码:

这是我的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);
        }
    }
}
package com.ayto.android.test;
导入android.content.Context;
导入android.graphics.RectF;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.RelativeLayout;
公共类noteLayout扩展了视图组{
public noteLayout(上下文活动上下文)
{
超级(activityContext);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
RelativeLayout mainParent=(RelativeLayout)getParent();
对于(int i=0;i
这是我的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基本上是来自activity_test3.xml的relativeLayout,它包含我创建的名为titleTextView的自定义视图。在这个视图中,我覆盖了onMeasure()方法,并使用staticLayout获取要设置的高度。然后我的relativeLayout(titleTextView的父级)基本上测量自身,并具有来自其子级titleTextView的约束(请参阅上面的代码以澄清任何混淆,因为很难解释)

我的问题是,相对身高比需要的要小,因此会剪掉孩子。以下是一些图片:

此外,您可以看到,不同设备的高度确实不一致,我认为staticLayout.getHeight()应该返回一致的像素大小。我不太确定如何修复它,希望得到帮助

注意:我已尝试将像素大小返回值乘以staticLayout.getHeight乘以密度,它变得太大,无法完全匹配其中的文本。

在自定义布局中将top(rect.top=20)偏移量添加到rect.bottom
onLayout
方法:

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

高度不一致是由于设备的像素密度不同。一些相关信息可以在

中找到。嘿,谢谢你的回答。这也是我的想法。高度不一致是由于设备上的像素密度不同,但奇怪的是,当我将像素返回乘以staticLayout时。高度()从密度上看,第一张图像中的设备与文本非常匹配。但是,第二张设备变得非常大,以至于里面的文本不需要它。我想我上面提到过用密度乘以它。
protected void onLayout(boolean changed, int l, int t, int r, int b) {
        .
        .
        .
        rect.bottom = childHeight + 20;