Android EditText 中的垂直内容对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3219253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:11:46  来源:igfitidea点击:

Vertical content align in Android EditText

androidlayoutandroid-edittextandroid-xmllayout-gravity

提问by BikeCurious

I have a multi-line EditText:

我有一个多行EditText

<EditText 
    android:layout_gravity="center"
    android:id="@+id/txtMessage"
    android:layout_height="wrap_content"
    android:layout_below="@+id/lblMessage"
    android:layout_width="wrap_content"
    android:width="250dip"
    android:text=""
    android:maxLength="760"
    android:lines="4">
</EditText>

Now the content is aligned to the middle. And I'd like to have the content aligned to the top.

现在内容对齐到中间。我想让内容与顶部对齐。

Is there a property to do that?

有物业可以这样做吗?

回答by Paresh Mayani

Try this: android:gravity="top"

尝试这个: android:gravity="top"

<EditText 
   android:layout_gravity="center"  
   android:id="@+id/txtMessage"
   android:layout_height="wrap_content" 
   android:layout_below="@+id/lblMessage"
   android:layout_width="wrap_content" 
   android:width="250dip"
   android:text="hello" 
   android:maxLength="760"
   android:gravity="top"                       
   android:lines="4"/>


so android:gravity="top"sets the text at the "Top" Position.


所以android:gravity="top"将文本设置在“顶部”位置。

You can also set different android:gravityattribute value such as center, bottom, center_vertical, left, right, center_horizontal, etc.

您还可以设置不同的android:gravity属性值,例如centerbottomcenter_verticalleftrightcenter_horizontal,等。

Enjoy !!

享受 !!

回答by alex.zherdev

Try

尝试

android:gravity="top"