java - 如何以编程方式正确调整 View 的大小,使其底部与 ConstraintLayout 中其他 View 的顶部对齐?

标签 java android

我在 ConstraintLayout 中有一个 ScrollView 和一个 FrameLayout。当我单击按钮时,我希望调整 ScrollView 布局的大小,以便 ScrollView 的底部与 FrameLayout 的顶部对齐。但是,单击按钮时约束不会改变。

我尝试将 ScrollView 放入 LinearLayout 中,但这也不起作用。

单击按钮时:

private void fixTableConstraint() {
        ConstraintLayout constraintLayout = findViewById(R.id.home_constraint_layout);
        View scrollView = findViewById(R.id.homeScreenScroll);
        ConstraintSet constraintSet = new ConstraintSet();

        constraintSet.clone(constraintLayout);
        constraintSet.connect(R.id.homeScreenScroll, ConstraintSet.BOTTOM, R.id.timeMenuOverlay, ConstraintSet.TOP, 0);
        constraintSet.applyTo(constraintLayout);

        ConstraintLayout.LayoutParams cl = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        scrollView.setLayoutParams(cl);
}

XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/home_constraint_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:id="@+id/homeScreenScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="0.2"
        tools:layout_editor_absoluteX="0dp">

        <TableLayout
            android:id="@+id/homescreenTable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </ScrollView>

    <FrameLayout
        android:id="@+id/timeMenuOverlay"
        android:layout_width="match_parent"
        android:layout_height="152dp"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <fragment
            android:id="@+id/timeMenuFragment"
            android:name="multicus.com.scheduleplannerv2.TimeMenuFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"/>
    </FrameLayout>

</android.support.constraint.ConstraintLayout>

正常布局: https://imgur.com/VsfSgVa

在我的情况下,单击按钮时会发生什么: /image/UmHg1.jpg

我真正想要的: /image/ztTZs.jpg

最佳答案

您的约束更改将不起作用,因为您有

ScrollView layout_height = “match_parent”

你可以这样做

<ScrollView
    android:id="@+id/homeScreenScroll"
    android:background="@color/green_dark"
    android:layout_width="match_parent"
    android:layout_height="0dp"            // this is important
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@id/timeMenuOverlay" // and this
    tools:layout_editor_absoluteX="0dp">

    <TableLayout
        ...
     />
</ScrollView>

<FrameLayout
    android:id="@+id/timeMenuOverlay"
    android:visibility="gone"               // !!
    android:layout_width="match_parent"
    android:layout_height="152dp"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

无需更改布局约束,只需更改可见性。

timeMenuOverlay 可见性 => GONE,将 ScrollView 展开到底部

timeMenuOverlay 可见性 => VISIBLE => 预期结果

关于java - 如何以编程方式正确调整 View 的大小,使其底部与 ConstraintLayout 中其他 View 的顶部对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943590/

相关文章:

java - 无法恢复 Activity 空指针异常和错误膨胀类 fragment ?

android - 如何在 Android 中禁用 EditText

java - 如何使用 guice 仅注入(inject)一个变量?

java - java.net.Socket 线程安全的方式是什么?

java - 像 .net 的 "Application_Start"和 "Begin_Request"for java/tomcat/JSP 这样的事件?

android - 是否有可能没有 Activity 的 fragment ?

java - 我如何从适配器获取数据并在 Android 的 Activity 中显示

java - Twitter4j,回复推文

android - 构建我的Android项目时无法合并dex错误

java - 向对等方发送 Hyperledger Fabric 的链码实例化请求时出现 OCI 运行时错误