How to scroll an EditText to the very top of the layout?(如何将 EditText 滚动到布局的最顶部?)
问题描述
所以我在滚动视图中有一些 ChipsInput 的布局,我只是希望这些在键盘被激活时转到最顶部
So I have this layout with some ChipsInput inside a scrollview and I simply want these to go to the very top when the keyboard is activated
我尝试将 windowSoftInputMode 更改为 adjustPan 等,但它并没有让它到达最顶端,只是没有覆盖它.
I've tried to change windowSoftInputMode to adjustPan and others but it does not make it to go to the very top, just doesn't cover it.
这是我的 xml 布局
Here's my xml layout
<LinearLayout 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/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background1"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<LinearLayout
android:id="@+id/layoutImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/colorPrimary"
android:elevation="12dp"
android:orientation="horizontal"
android:padding="10dp"
tools:targetApi="lollipop">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="3dp"
android:layout_weight="1"
android:fontFamily="@font/quattrocento"
android:gravity="left"
android:text="@string/profile"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="@color/textColor1"
tools:ignore="RtlHardcoded" />
<ImageButton
android:id="@+id/btUpdate"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="@color/colorPrimary"
android:src="@drawable/arrow_right_icon" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="5dp"
android:orientation="vertical"
tools:targetApi="o">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="@font/quattrocento"
android:gravity="center"
android:text="@string/gallery"
android:textSize="22sp"
android:textStyle="bold"
android:textColor="@color/textColor2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
android:padding="5dp"
android:baselineAligned="false">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageButton
android:id="@+id/close1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_close"
android:background="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:elevation="7dp"
android:visibility="invisible"/>
<ImageButton
android:id="@+id/add1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_add"
android:background="@color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:elevation="7dp"/>
<ImageView
android:id="@+id/photo1"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginHorizontal="5dp"
android:background="@drawable/rounded_rect_primary_dark"
android:elevation="5dp"
android:scaleType="fitXY"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageButton
android:id="@+id/close2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_close"
android:background="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:elevation="7dp"
android:visibility="invisible"/>
<ImageButton
android:id="@+id/add2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_add"
android:background="@color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:elevation="7dp"/>
<ImageView
android:id="@+id/photo2"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginHorizontal="5dp"
android:background="@drawable/rounded_rect_primary_dark"
android:elevation="5dp"
android:scaleType="fitXY" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<ImageButton
android:id="@+id/close3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_close"
android:background="@color/white"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:elevation="7dp"
android:visibility="invisible"/>
<ImageButton
android:id="@+id/add3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_add"
android:background="@color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:elevation="7dp"/>
<ImageView
android:id="@+id/photo3"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginHorizontal="5dp"
android:background="@drawable/rounded_rect_primary_dark"
android:elevation="5dp"
android:scaleType="fitXY"/>
</RelativeLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginHorizontal="12dp"
android:layout_marginVertical="5dp"
android:background="@color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/quattrocento"
android:text="@string/about_me"
android:textColor="@color/textColor2"
android:textSize="20sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/white">
<EditText
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="@color/white"
android:focusable="true"
android:maxLength="500"
android:inputType="text"
android:importantForAutofill="no"
android:autofillHints="no"
tools:ignore="LabelFor" />
<TextView
android:id="@+id/caracCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/quattrocento"
android:text="500"
android:textColor="@color/black"
android:textSize="16sp"
android:layout_gravity="end" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/quattrocento"
android:text="@string/interest1"
android:textColor="@color/textColor2"
android:textSize="20sp"
android:layout_gravity="center"
android:gravity="center"/>
<Spinner
android:id="@+id/categories1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/colorPrimary" />
</LinearLayout>
<com.pchmn.materialchips.ChipsInput
android:id="@+id/chipsInput1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/white"
android:elevation="3dp"
android:focusable="false"
app:chip_deletable="true"
app:chip_hasAvatarIcon="false"
app:hint="#"
app:chip_backgroundColor="@color/colorPrimary"
app:chip_deleteIconColor="@color/black"
app:chip_labelColor="@color/black"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/quattrocento"
android:text="@string/interest2"
android:textColor="@color/textColor2"
android:textSize="20sp"
android:layout_gravity="center"
android:gravity="center"/>
<Spinner
android:id="@+id/categories2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:background="@color/colorPrimary" />
</LinearLayout>
<com.pchmn.materialchips.ChipsInput
android:id="@+id/chipsInput2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="@color/white"
android:elevation="3dp"
android:focusable="false"
app:chip_deletable="true"
app:chip_hasAvatarIcon="false"
app:hint="#"
app:chip_backgroundColor="@color/colorPrimary"
app:chip_deleteIconColor="@color/black"
app:chip_labelColor="@color/black" />
<Button
android:id="@+id/btLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="25dp"
android:background="@color/colorPrimary"
android:elevation="12dp"
android:fontFamily="@font/quattrocento"
android:text="@string/logout"
android:textSize="20sp"
android:onClick="logout"
tools:targetApi="lollipop"
android:textColor="@color/black"/>
</LinearLayout>
</ScrollView>
我只需要将它发送到布局的最顶部,这样当用户开始输入时我的可过滤列表就会出现.
I simply need it to be sent to the very top of the layout so my filterable list will appear when the user start to type.
推荐答案
你可以使用CoordinatorLayout 作为你的根和 NestedScrollView 在里面.
You can use CoordinatorLayout as your root and NestedScrolView in it .
CoordinatorLayout 作为 root 对键盘做出反应,并使用 NestedScrollView 助手将您的布局滚动到手机顶部,您的 NestedScrollView 包含您的布局代码.
CoordinatorLayout as root reacts to keyboard and scrolls your layout to the top of your phone with assistant of NestedScrollView which your NestedScrollView includes your layout code .
把问题放在透视图中
CoordinaterLayout > NestedScrollView > yourLayout
改变你的布局 XMl 像下面的代码
Change your layout XMl like code below
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background1"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical">
<!-- rest of your layout xml code-->
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
这篇关于如何将 EditText 滚动到布局的最顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 EditText 滚动到布局的最顶部?
基础教程推荐
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
