Android LayoutParams未正确更新

Android LayoutParams未正确更新,android,android-layout,kotlin,Android,Android Layout,Kotlin,所以我有一个滚动视图,我需要调整滚动视图的高度,以确保它保持在模式弹出视图之上。我无法使用约束布局,因为此模式弹出视图不是同一视图父视图的子视图。因此,我尝试动态更新我的滚动视图布局参数,使其高度足够小,不会隐藏在模式弹出窗口后面 弹出视图的高度可以在点处更改,因此我有一个回调,在模态视图更改时返回新高度。在该回调中,我调整了滚动视图的高度,如下所示: someModalView.onHeightChanged = { newViewHeight -> Log.d("TESTHEI

所以我有一个滚动视图,我需要调整滚动视图的高度,以确保它保持在模式弹出视图之上。我无法使用约束布局,因为此模式弹出视图不是同一视图父视图的子视图。因此,我尝试动态更新我的滚动视图布局参数,使其高度足够小,不会隐藏在模式弹出窗口后面

弹出视图的高度可以在点处更改,因此我有一个回调,在模态视图更改时返回新高度。在该回调中,我调整了滚动视图的高度,如下所示:

someModalView.onHeightChanged = { newViewHeight ->
    Log.d("TESTHEIGHT", "PreHeight = ${scrollView.height}")
    scrollView.layoutParams = FrameLayout.LayoutParams(scrollView.width, scrollView.height - newViewHeight)
    scrollView.requestLayout()
    Log.d("TESTHEIGHT", "PostHeight = ${scrollView.height}")
}
someModalView.onHeightChanged = { newViewHeight ->
    scrollView.setPadding(0, 0, 0, newViewHeight)
}
不幸的是,上面的代码似乎什么也没做,在我的日志中,我可以看到PreHeight打印的高度与postweight相同。有没有理由不改变视图高度

另外,我对它进行了调试,并确保newViewHeight不是0,也不是,而是~800


通过向视图中添加填充而不是像这样更改其高度,最终使其正常工作:

someModalView.onHeightChanged = { newViewHeight ->
    Log.d("TESTHEIGHT", "PreHeight = ${scrollView.height}")
    scrollView.layoutParams = FrameLayout.LayoutParams(scrollView.width, scrollView.height - newViewHeight)
    scrollView.requestLayout()
    Log.d("TESTHEIGHT", "PostHeight = ${scrollView.height}")
}
someModalView.onHeightChanged = { newViewHeight ->
    scrollView.setPadding(0, 0, 0, newViewHeight)
}

这正是我所需要的,但是它并没有真正回答这个问题,所以我将把它留在答案中,留给其他可能有帮助的人。但是如果知道为什么更改布局参数不会更新视图高度,那还是很好的。

试试看它是否适合您

val params = scrollView.layoutParams;
params.height = scrollView.height - newViewHeight
scrollView.layoutParams = params

试着看看它是否适合你

val params = scrollView.layoutParams;
params.height = scrollView.height - newViewHeight
scrollView.layoutParams = params

有一次,我需要获取软键盘的高度来更新我的视图:

    public class MainActivity extends AppCompatActivity {

    private ScrollView sView;
    private int heightDiff;

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


        sView = findViewById(R.id.scrollView);
        //Here we get the height of soft keyboard by observing changes of softKeyboard height.
        sView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                heightDiff = sView.getRootView().getHeight() - sView.getHeight();
            }
        });


        final EditText email = findViewById(R.id.eemail);
        EditText firstName = findViewById(R.id.efirstname);
        firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!isVisibleWhileSoftKeyboardShowing(email) && hasFocus) {
                    sView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            sView.smoothScrollBy(0, 200);
                        }
                    }, 500);
                }

            }
        });
    }

    /**
     * check if a view is currently visible in the screen or not
     *
     * @param view
     * @return
     */
    public boolean isVisibleWhileSoftKeyboardShowing(final View view) {
        if (view == null) {
            return false;
        }
        if (!view.isShown()) {
            return false;
        }
        final Rect actualPosition = new Rect();
        view.getGlobalVisibleRect(actualPosition);
        final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight() - heightDiff);
        return actualPosition.intersect(screen);
    }


    /**
     * to get screen width
     *
     * @return
     */
    public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    /**
     * to get screen height
     *
     * @return
     */
    public static int getScreenHeight() {
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }
}

heightDiff是软键盘的高度。有2个编辑文本。我想滚动,如果软键盘隐藏了较低的一个。希望这与您的情况类似。

一旦我需要获取软键盘的高度以更新视图:

    public class MainActivity extends AppCompatActivity {

    private ScrollView sView;
    private int heightDiff;

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


        sView = findViewById(R.id.scrollView);
        //Here we get the height of soft keyboard by observing changes of softKeyboard height.
        sView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                heightDiff = sView.getRootView().getHeight() - sView.getHeight();
            }
        });


        final EditText email = findViewById(R.id.eemail);
        EditText firstName = findViewById(R.id.efirstname);
        firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!isVisibleWhileSoftKeyboardShowing(email) && hasFocus) {
                    sView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            sView.smoothScrollBy(0, 200);
                        }
                    }, 500);
                }

            }
        });
    }

    /**
     * check if a view is currently visible in the screen or not
     *
     * @param view
     * @return
     */
    public boolean isVisibleWhileSoftKeyboardShowing(final View view) {
        if (view == null) {
            return false;
        }
        if (!view.isShown()) {
            return false;
        }
        final Rect actualPosition = new Rect();
        view.getGlobalVisibleRect(actualPosition);
        final Rect screen = new Rect(0, 0, getScreenWidth(), getScreenHeight() - heightDiff);
        return actualPosition.intersect(screen);
    }


    /**
     * to get screen width
     *
     * @return
     */
    public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }

    /**
     * to get screen height
     *
     * @return
     */
    public static int getScreenHeight() {
        return Resources.getSystem().getDisplayMetrics().heightPixels;
    }
}

heightDiff是软键盘的高度。有2个编辑文本。我想滚动,如果软键盘隐藏了较低的一个。希望这与你的情况类似。

似乎给了我同样的结果似乎给了我同样的结果