【问题标题】:Calculate the height i dp got wrong计算我 dp 错误的高度
【发布时间】:2020-04-04 14:45:38
【问题描述】:

这里的主要问题是父布局是嵌套滚动视图所以高度必须是包裹内容 所以视图就像第一张图片

我想要的是制作“没有帐户?注册”TextView 在屏幕底部,所以我计算我的高度 电话然后布局的高度减去减去它们和 将结果分配给 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 应该正好在底部

【问题讨论】:

  • 您可以查看正确答案

标签: java android android-layout layout


【解决方案1】:

我建议您将 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>

【讨论】:

    【解决方案2】:

    试试这个:

    <?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>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 2020-03-15
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 2019-07-20
      • 1970-01-01
      相关资源
      最近更新 更多