有一个带有 EditText 的 fragment ,我想在这个 fragment 的顶部显示一个固定的工具栏。
我希望这个工具栏显示在顶部,无论我在 EditText 上按了多少次 enter,但现在它只是向上滚动并消失了。
我该如何解决?
这是 fragment 的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".MemoFragment">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="60dp">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/memo_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:layout_collapseMode="pin">
<Button
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_shape"
android:text="Confirm"
android:textColor="@android:color/white"
android:textSize="@dimen/text_size_medium"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
<EditText
android:id="@+id/memo_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/text_shape"
android:fontFamily="sans-serif"
android:gravity="top" />
</RelativeLayout>
我已经尝试了在 StackOverflow 上找到的许多解决方案,但无法解决。 谁能帮我这个? 谢谢。
最佳答案
如果你还是看不懂@Parul的评论,
这里是更新的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context=".MemoFragment">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapseActionView"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="60dp">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/memo_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:layout_collapseMode="pin">
<Button
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_shape"
android:text="Confirm"
android:textColor="@android:color/white"
android:textSize="@dimen/text_size_medium"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
<EditText
android:id="@+id/memo_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/collapseActionView"
android:layout_marginLeft="@dimen/spacing_medium"
android:background="@drawable/text_shape"
android:fontFamily="sans-serif"
android:gravity="top" />
</RelativeLayout>
这里发生的事情是您需要在相对布局中定义特定位置,否则稍后绘制/定义的 View (您的编辑文本)将显示在先前绘制的 View (您的工具栏)之上。
关于android - 在 fragment 中的 EditText 中按回车键时修复顶部的工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52659632/