Android Edit文本对齐 [英] Android Edit text alignment

查看:135
本文介绍了Android Edit文本对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为平板电脑制作一个小型应用程序,类似于显示标志.

I am making a small app for a tablet, something like a display sign.

我有几个在显示符号中居中对齐的文本元素.当我点击它时,它会转换为editText.我希望以这样一种方式编辑文本,使其中的文本居中对齐并包装内容.但是,当我输入某些内容时,editText会同时展开两种方式,从而使文本与中心对齐.我想对右对齐的文本元素执行相同的操作(唯一的不同是,编辑文本只会向左扩展).

I have a few text elements that are center aligned in the display sign. When I tap on it, it gets converted to editText. I want the edit text in such a way that the text in it is center aligned and content wrapped. But when i input something, editText expands both ways, keeping the text aligned to the center. I want to do the same thing with right aligned text elements (only difference is that the edit text would expand only towards the left).

当我现在尝试执行此操作时,它会向左扩展.这是我用于布置Edit Text元素的代码

When i am trying to do this right now it expands towards the left.This is my code for laying out the edit Text element

tempView=new EditText(this);
((EditText) tempView).setTextSize(shrinkedFontSize);
((EditText) tempView).setTypeface(tf);
((EditText) tempView).setText(tempInput.getText());     
((EditText) tempView).setGravity(Gravity.CENTER);
layout.addView(tempView, tempParams);

我在布局参数中将布局的宽度设置为Wrap_content.我还在布局参数中为编辑文本的左上角指定了x和y.

I am setting the width of the layout as Wrap_content in the layout parameters. I have also specified the x and y in my layout parameters for the top left corner of the edit text.

有什么办法可以做到这一点?

Is there any way to achieve this ?

推荐答案

这应该可以解决您的问题:

This should resolve your problem:

EditText t = (EditText) findViewById(R.id.text);
t.setGravity(Gravity.CENTER);

...在XML中:

<EditText
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="text" />

这使EditText inputField输入的文本水平居中.

It makes the EditText inputField entered text centered horizontally.

您可以将layout_widthfill_parent更改为150 dp或任何其他大小,但不能将wrap_content更改.文本将水平居中.

You can change the layout_width from fill_parent to 150 dp or any other size, but not wrap_content. The text will be centered horizontally.

设置t.setGravity(Gravity.RIGHT);LEFT,使输入的文本左对齐或右对齐.

Set the t.setGravity(Gravity.RIGHT); or LEFT, that makes entered text left or right alignment.

这篇关于Android Edit文本对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆