编辑文本对齐

发布于 2024-12-12 05:42:58 字数 853 浏览 0 评论 0原文

我正在构建一个简单的文本编辑器。我想将文本左对齐、居中对齐、右对齐。

我做了以下操作,但没有更改文本的对齐方式

我该怎么做?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-12-19 05:42:58

首先,LinearLayout.LayoutParams 构造函数不将重力作为参数 - 第三个参数是布局权重。
LinearLayout.LayoutParams 的字段 gravity 实际上是layout_gravity,它定义了其父级内部的布局对齐方式。

你需要的是

if (command == 0 )
{
    txt.setGravity(Gravity.TOP | Gravity.LEFT);
} 

例如。

First of all, LinearLayout.LayoutParams constructor does not take gravity as a parameter - third parameter is layout weight.
Field gravity of LinearLayout.LayoutParams is in fact layout_gravity, which defines layout aligment inside it's parent.

What you need is

if (command == 0 )
{
    txt.setGravity(Gravity.TOP | Gravity.LEFT);
} 

for example.

抠脚大汉 2024-12-19 05:42:58

您需要做的就是直接在 EditText 上使用方法 setGravity() ,如下所示:

EditText txt = (EditText) findViewById(R.id.txt_gcFrontInsideTextData);
switch(command)
{
  case 0:
    txt.setGravity(Gravity.TOP | Gravity.LEFT);
    break;
  case 1:
    txt.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    break;
  case 2:
    txt.setGravity(Gravity.TOP | Gravity.RIGHT);
    break;
}

All you need to do is use the method setGravity() directly on the EditText like this:

EditText txt = (EditText) findViewById(R.id.txt_gcFrontInsideTextData);
switch(command)
{
  case 0:
    txt.setGravity(Gravity.TOP | Gravity.LEFT);
    break;
  case 1:
    txt.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
    break;
  case 2:
    txt.setGravity(Gravity.TOP | Gravity.RIGHT);
    break;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文