Java 计算出我弄错的高度

Java 计算出我弄错的高度,java,android,android-layout,layout,Java,Android,Android Layout,Layout,这里的主要问题是父布局是嵌套的滚动视图,因此高度必须为“包裹内容” 因此,视图就像第一幅图像 我想要的是使“没有帐户?注册”文本视图 在屏幕的底部,所以我计算我的眼睛的高度 然后用手机的高度来减去布局来减去它们 将结果分配到textview的上页边距 这是我的密码 ViewTreeObserver observer = binding.parentConstraint.getViewTreeObserver(); if (observer.isAlive()) {

这里的主要问题是父布局是嵌套的滚动视图,因此高度必须为“包裹内容” 因此,视图就像第一幅图像

我想要的是使“没有帐户?注册”文本视图 在屏幕的底部,所以我计算我的眼睛的高度 然后用手机的高度来减去布局来减去它们 将结果分配到textview的上页边距

这是我的密码

ViewTreeObserver observer = binding.parentConstraint.getViewTreeObserver();
        if (observer.isAlive()) {
            observer.addOnGlobalLayoutListener(() -> {
                DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
                float parentViewHeightInDP = (int) (binding.parentConstraint.getHeight() / displayMetrics.density);
                if (getContext() != null) {
                    float heightInDP = displayMetrics.heightPixels / displayMetrics.density;

                    float distanceBetweenLayoutBottomAndParenBottom =  heightInDP - parentViewHeightInDP;
                    if (distanceBetweenLayoutBottomAndParenBottom > 32) {
                        if (topMargin == 0) {
                            topMargin = 1;
                            ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) binding.loginTextDontHaveAccount.getLayoutParams();
                            layoutParams.topMargin = Math.round(distanceBetweenLayoutBottomAndParenBottom);
                            Handler handler = new Handler();
                            handler.postDelayed(() -> {
                                binding.loginTextDontHaveAccount.requestLayout();                            }, 
50);
                    }
                }
但这就是我得到的结果

所以我真正的问题是为什么这里还有一些空间,我是说 TextView应该正好位于底部


我建议您将NestedScrollView作为子级而不是父级。 将RelativeLayout设置为父级,并使用layout_alignParentBottom在底部设置TextView。像这样的

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

    <androidx.core.widget.NestedScrollView 
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="?android:colorBackground"
         android:clickable="true"
         android:fillViewport="true"
         android:focusable="true">

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

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/img1"
                android:layout_width="wrap_content"
                android:layout_height="650dp"
                android:background="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="650dp"
                android:layout_below="@+id/img1"
                android:background="@color/colorPrimary" />
        </LinearLayout>
    </RelativeLayout>
 </androidx.core.widget.NestedScrollView>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />
 </RelativeLayout>

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical">

            <AutoCompleteTextView
                android:id="@+id/emailtext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:hint="Email"
                android:inputType="textEmailAddress"
                android:textColorHint="#80000000" />

            <EditText
                android:id="@+id/passwordtext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:hint="Password"
                android:inputType="textPassword"
                android:textColorHint="#80000000" />

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center">

                <Button
                    android:id="@+id/enter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Enter"
                    android:textAllCaps="false"
                    app:layout_constraintBottom_toTopOf="@id/textView"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="This is my app"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/enter" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>


可以使用约束布局。注意文本视图的布局约束按钮和布局约束按钮。

您可以检查正确答案