Android EditText使用标题后的整个活动高度



我正在开发一个android应用程序,以支持用户发布文章,并为其提供EditText。下面是我的布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/article_title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write Article"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/article_text_field"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

问题是,在点击EditText时,文章标题在视图中变成了半截,EditText的光标在底部,应该在顶部。

有人对如何修复它有建议吗?

您必须为TextInputEditText设置重力,并删除android:gravity="vertical"

它应该是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/article_title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write Article"
/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/article_text_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

现在,当你有很多行时,你的布局将在写作时滚动,带有标题的应用程序栏将开始隐藏。我认为这很好,因为用户会看到更多的EditText,所以写长文本会更容易

如果您希望TextView和TextInputLayout水平显示(基于LinearLayout属性android:orientation="vertical"(,那么您可以通过以下方式实现所需布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
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">
<TextView
android:id="@+id/article_title_text_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="Write Article"
android:padding="12dp"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/article_text_field"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/article_title_text_view"
android:layout_alignParentEnd="true"
app:hintEnabled="false">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:gravity="top|start"
android:padding="12dp"
tools:text="@tools:sample/lorem/random"/>
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>

如果您想使TextView和TextInputLayout垂直对齐,请使用:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout   
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">
<TextView
android:id="@+id/article_title_text_view"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:padding="12dp"
android:text="Write Article"
android:textAlignment="center"/>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/article_text_field"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/article_title_text_view"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
app:hintEnabled="false">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:gravity="top|start"
android:padding="12dp"
tools:text="@tools:sample/lorem/random"/>
</com.google.android.material.textfield.TextInputLayout>
</RelativeLayout>

实际上,您应该在com.google.android.material.textfield.TextInputEditText中使用android:inputType="textMultiLine"属性,并指定要为字段提供的行数
因为字段是一个文章,意味着一个多行字段。此外,请将方向指定为垂直方向
,代码将如下所示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/article_title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write Article" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/article_text_field"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:inputType="textMultiLine"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

最新更新