如何解决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>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。