android ConstraintLayout高度百分比0.99 [英] android ConstraintLayout height percentage 0.99

查看:209
本文介绍了android ConstraintLayout高度百分比0.99的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当app:layout_constraintHeight_percent为0.50或0.80时,它可以正常工作,但是当我将其设置为0.99时,Button会变得很长并且大于屏幕的99%?

When app:layout_constraintHeight_percent is 0.50 or 0.80 it works fine, but when I set it to 0.99 , the Button go very long and larger than 99% of screen ?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fillViewport="true"
    android:orientation="vertical">


    <android.support.constraint.ConstraintLayout
        android:orientation="vertical"
        android:background="@color/sos"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <Button
            android:id="@+id/mybtn1"
            android:text="1"
            android:layout_width="match_parent"
            app:layout_constraintHeight_percent="0.99"
            android:layout_height="0dp" />

        <Button
            app:layout_constraintTop_toBottomOf="@+id/mybtn1"
            android:text="2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


    </android.support.constraint.ConstraintLayout>



</ScrollView>

推荐答案

问题不在于99%layout_constraintHeight_percent,而是当两个按钮的组合高度超过ScrollView的屏幕高度时.碰巧有99%的人在您的模拟器/设备上触发了这种任性的行为.如果您将第二个按钮调高得多,则百分比会变小.

The problem is not with the 99% layout_constraintHeight_percent but when the combined height of the two buttons exceeds the height the on-screen height of the ScrollView. It just happens that 99% triggers this wayward behavior on your emulator/device. If you make the second button much taller, you will see the same behavior with smaller percentages.

我发现这些类型的布局存在问题,其中孩子的尺寸取决于父母的大小,而父母的大小取决于孩子的大小.在您的布局中,子级mybtn1取决于父级ConstraintLayout(app:layout_constraintHeight_percent="0.99"),而父级ConstraintLayout取决于子级mybtn1(android:layout_height="wrap_content")的大小.当两个按钮的组合高度未超过ScrollView的屏幕高度时,这似乎可以正常工作,但是当组合高度超过其高度时,它会失败. (当组合高度小于ScrollView高度时,在ScrollView上设置android:fillViewport="true"可能会起作用,因为ScrollView有一个隐式的固定高度,这只是屏幕上的可用空间.不过,这只是个推测.)

I have found these types of layouts to be problematic where a child's measurements depend upon the size of the parent and the parent size depends upon the size of the child. In your layout, the child mybtn1 depends upon the parent ConstraintLayout (app:layout_constraintHeight_percent="0.99") while the parent ConstraintLayout depends upon the size of the child mybtn1 (android:layout_height="wrap_content"). This seems to work OK when the combined heights of the two buttons do not exceed the on-screen height of the ScrollView but fails miserably when the combined heights do exceed it. (Setting android:fillViewport="true" on the ScrollView when the combined heights are less than the ScrollView height may work because there is an implicit fixed height to the ScrollView which is just the available real estate on the screen. This is just conjecture, though.)

您可以通过以下一些编码来解决此问题:

You can fix this with a little coding as follows:

ScrollView添加一个ID:

android:id="@+id/scrollView"

将以下代码放入onCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ScrollView scrollView = findViewById(R.id.scrollView);
    scrollView.post(new Runnable() {
        @Override
        public void run() {
            Button button = findViewById(R.id.mybtn1);
            ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) button.getLayoutParams();

            // Get percentage height of button.
            float percent = lp.matchConstraintPercentHeight;

            // Explicitly set the button height based upon the ScrollView height.
            button.setHeight((int) (scrollView.getHeight() * percent));

            // Reset the percent height so it no longer has any effect.
            lp.matchConstraintPercentHeight = 1.0f;
        }
    });
}

此代码将强制mybtn1上的大小为ScrollView高度的99%或为按钮指定的百分比.

This code will force a size on mybtn1 that is 99% of the ScrollView height or whatever percentage is specified for the button.

这篇关于android ConstraintLayout高度百分比0.99的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆