单击EditText时,布局没有足够向上移动 [英] Layout doesn't move up enough when clicking EditText

查看:87
本文介绍了单击EditText时,布局没有足够向上移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击EditText时,视图确实向上移动.但这还不够.它应上移到someId,在someId下方应显示按钮,在其下方应显示keyboard.为什么这行不通?

The view does move up when clicking in the EditText. But it doesn't move up enough. It should move up to someId, below someId the button should be displayed and below that the keyboard should be shown. Why doesn't this work?

Android清单:

<activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize">

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="400dp"
                android:background="@android:color/holo_red_dark" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="132dp"
                android:padding="8dp"
                android:background="@android:color/holo_green_dark"/>

            <View
                android:id="@+id/someId"
                android:layout_width="match_parent"
                android:layout_height="20dp" />

        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_send"
            style="@style/Widget.AppCompat.Button.Borderless"
            android:layout_width="0dp"
            android:layout_height="?attr/actionBarSize"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:clickable="false"
            android:text="Test"
            android:textColor="@color/defaultGreen"
            android:textSize="18sp" />

    </LinearLayout>
</RelativeLayout>

推荐答案

在视图上滚动到底部的方式如何

How about scrolling to bottom on view changed

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

  final EditText editText = (EditText) findViewById(R.id.editText);
  final ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);

  editText.getRootView().addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
      //Scroll to bottom of view
      scrollView.smoothScrollTo(0, editText.getBottom());
    }
  });

}

这篇关于单击EditText时,布局没有足够向上移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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