【问题标题】:Android Kotlin set height of FrameLayout programatically not workAndroid Kotlin 以编程方式设置 FrameLayout 的高度不起作用
【发布时间】:2021-07-28 21:34:19
【问题描述】:

我正在尝试:

        fragment_container.layoutParams.height = 100
        fragment_container.requestLayout()

        val params = fragment_container.layoutParams
        params.height = 100
        fragment_container.layoutParams = params
        fragment_container.requestLayout()

两者都不起作用,这个fragment_container(它是FrameLayout)的高度仍然像在layout.xml中声明的那样

如果需要,这里是布局:

<?xml version="1.0" encoding="utf-8"?>
<pl.jawegiel.exchangerates.ClickableMotionLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:motion="http://schemas.android.com/apk/res-auto"
    android:id="@+id/motion_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    motion:layoutDescription="@xml/main_activity_scene">

    <ImageView
        android:id="@+id/iv_header"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="@mipmap/exchange_rates"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toTopOf="parent"/>

    <LinearLayout
        android:id="@+id/ll_toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/teal_700"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:padding="8dp"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintStart_toStartOf="parent"
        motion:layout_constraintTop_toBottomOf="@+id/iv_header">

        <RelativeLayout
            android:id="@+id/rl_inside_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/toolbar_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textSize="20sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/toolbar_subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/toolbar_title"
                android:textSize="12sp"
                android:textStyle="bold" />
        </RelativeLayout>
    </LinearLayout>

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="400dp"/>

    <TextView
        android:id="@+id/about"
        android:layout_width="wrap_content"
        android:layout_height="?attr/actionBarSize"
        android:gravity="center_vertical"
        android:text="@string/about"
        android:layout_marginEnd="10dp"
        android:textSize="18sp"
        android:visibility="visible"
        motion:layout_constraintEnd_toEndOf="parent"
        motion:layout_constraintTop_toTopOf="parent" />
</pl.jawegiel.exchangerates.ClickableMotionLayout>

你还需要知道什么?还有什么事情你需要知道?我想我已经添加了您需要的所有内容,但以防万一。提前谢谢!

【问题讨论】:

    标签: android kotlin android-layout


    【解决方案1】:

    在@femi mcpd 的更正之后,它几乎可以工作,但还剩下一件事。当我将您的代码放入此方法时:

    private fun initToolbarListener() {
        ll_toolbar.addOnLayoutChangeListener { view, _, _, _, _, _, _, _, _ ->
            llHeaderY =  view.y
    
            val frameLayout = findViewById<FrameLayout>(R.id.fragment_container)
            frameLayout.layoutParams.height = 400
        }
    

    它不起作用,但我真的很想这样做,但如果我将它直接放在 onCreate 中它会起作用。为什么?

    【讨论】:

    • 有趣,如何触发ll_toolbar布局的变化?我可以看看你的片段代码吗
    【解决方案2】:

    终于全部解决了。我需要用RelativeLayout包装fragment_contaner,并像这样修改这个方法:

    private fun initToolbarListener() {
        ll_toolbar.addOnLayoutChangeListener { view, _, _, _, _, _, _, _, _ ->
            llToolbarY = view.y.toInt()
    
            fragment_container.postDelayed({
                val params = fragment_container.layoutParams as RelativeLayout.LayoutParams
                params.height = getHeightOfScreenWithoutBars() - llToolbarY - ll_toolbar.height
                fragment_container.layoutParams = params
            }, 100)
        }
    }
    

    再一次谢谢你!我接受这个问题是为了你的承诺:) 亲切的问候

    【讨论】:

      猜你喜欢
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多