编辑文本对齐
我正在构建一个简单的文本编辑器。我想将文本左对齐、居中对齐、右对齐。
我做了以下操作,但没有更改文本的对齐方式
我该怎么做?
EditText txt = (EditText) findViewById(R.id.txt_gcFrontInsideTextData);
if (command == 0 )
{
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.TOP | Gravity.LEFT);
txt.setLayoutParams(p);
}
else if (command == 1)
{
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
txt.setLayoutParams(p);
}
else if (command == 2)
{
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.TOP | Gravity.RIGHT);
txt.setLayoutParams(p);
}
I am building a simple text editor. I wanted to align text to left, center and right.
I did following, but does not change the alignment of the text
How do I do this?
EditText txt = (EditText) findViewById(R.id.txt_gcFrontInsideTextData);
if (command == 0 )
{
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.TOP | Gravity.LEFT);
txt.setLayoutParams(p);
}
else if (command == 1)
{
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL);
txt.setLayoutParams(p);
}
else if (command == 2)
{
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, Gravity.TOP | Gravity.RIGHT);
txt.setLayoutParams(p);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
LinearLayout.LayoutParams
构造函数不将重力作为参数 - 第三个参数是布局权重。LinearLayout.LayoutParams
的字段gravity
实际上是layout_gravity,它定义了其父级内部的布局对齐方式。你需要的是
例如。
First of all,
LinearLayout.LayoutParams
constructor does not take gravity as a parameter - third parameter is layout weight.Field
gravity
ofLinearLayout.LayoutParams
is in fact layout_gravity, which defines layout aligment inside it's parent.What you need is
for example.
您需要做的就是直接在
EditText
上使用方法setGravity()
,如下所示:All you need to do is use the method
setGravity()
directly on theEditText
like this: