简体   繁体   English

如何以编程方式获得线性布局的正确高度?

[英]How to get correct height of Linear layout programmatically?

Here i set the root layout height as android:layout_height="400dp" .在这里,我将根布局高度设置为android:layout_height="400dp"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/container"
android:orientation="vertical"
android:layout_height="400dp"
tools:context=".FirstFragment">


<ImageButton
    android:id="@+id/sleep_timer_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher_background"
    android:gravity="center_vertical|center_horizontal"
    android:padding="20dp"
    android:scaleType="fitXY"
    android:src="@drawable/ic_launcher_background"
    app:layout_constraintBottom_toBottomOf="@+id/guideline31"
    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintEnd_toEndOf="@+id/guideline24"
    app:layout_constraintStart_toStartOf="@+id/guideline23"
    app:layout_constraintTop_toTopOf="@+id/guideline30" />
<TextView
    android:id="@+id/textview_first"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_first_fragment"
    app:layout_constraintBottom_toTopOf="@id/button_first"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<Button
    android:id="@+id/button_first"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/next"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/textview_first" />
</LinearLayout>

My case i want to receive the container height in programmatically(400dp).我的情况我想以编程方式(400dp)接收容器高度。 i try with all methods我尝试了所有方法

container.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
container.measure(View.MeasureSpec.AT_MOST, View.MeasureSpec.AT_MOST)
container.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY)
val height = measuredHeight

and also并且

    val viewTreeObserver: ViewTreeObserver = container.viewTreeObserver
    if (viewTreeObserver.isAlive) {
        viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                container.viewTreeObserver.removeOnGlobalLayoutListener(this)
                val viewHeight: Int = container.height
                val viewHeight1: Int = container.measuredHeight
            }
        })
     }

Nothing works.how to get the container height(400) programmatically?什么都不起作用。如何以编程方式获取容器高度(400)?

first of all you need a method to convert px to dp :首先,您需要一种将 px 转换为 dp 的方法:

public float convertPixelsToDp(float px, Context context){
    return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}

then you can get height of your LinearLayout like this and show that in a Toast:那么你可以像这样获得你的 LinearLayout 的高度并在 Toast 中显示它:

final LinearLayout layout = findViewById(R.id.container);

    ViewTreeObserver vto = layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            int width  = layout.getMeasuredWidth(); // this method return px value
            int height = layout.getMeasuredHeight(); // this method return px value

            Toast.makeText(MainActivity.this, "height is : " + convertPixelsToDp(height, MainActivity.this), Toast.LENGTH_LONG).show();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM