【问题标题】:android-TextView setGravity doesn't workingandroid-TextView setGravity 不起作用
【发布时间】:2014-07-19 03:13:09
【问题描述】:

我有一个问题。我正在尝试创建具有规格尺寸的 TextView,并且我在 onMeasure 方法中执行此操作。我的问题是我想更改 textalignment,所以我调用 setGravity(Gravity.RIGHT) 方法但它不是工作,文本仍在左侧。

以下是我的代码,希望有人能帮助我:(

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout layout = new LinearLayout(this);
    MyLabel label = new MyLabel(this);
    label.setText("Hello");
    label.setBackgroundColor(Color.RED);
    label.setGravity(Gravity.RIGHT);
    layout.addView(label);

    setContentView(layout);

}

public class MyLabel extends TextView {

    public MyLabel(Context context) {
        super(context);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // Desire Size : Mac dinh la Wrap Content
        float desireWidth = getMeasuredWidth();
        float desireHeight = getMeasuredHeight();

        int maxWidth = 0;
        int maxHeight = 0;
        float width = 0.7f;
        float height = 0.3f;

        if (maxWidth == 0) {
            maxWidth = MeasureSpec.getSize(widthMeasureSpec);
        }

        if (maxHeight == 0) {
            maxHeight = MeasureSpec.getSize(heightMeasureSpec);
        }

        // Neu abstract model co thong tin ve kich thuoc :
        if (width > 0) {

            int parentWidth = 0;
            if (true) {
                parentWidth = maxWidth;
            } else {
                parentWidth = 480;
            }
            if (width <= 1) {
                // Gia tri duoc tinh toan theo % cua parent
                desireWidth = width * parentWidth;
            } else {
                desireWidth = width < parentWidth ? width : parentWidth;
            }
        }

        // Kich thuoc chieu cao cung tuong tu nhu vay
        if (height > 0) {

            int parentHeight = 0;
            if (true) {
                parentHeight = maxHeight;
            } else {
                parentHeight = 800;
            }
            if (height <= 1) {
                // Gia tri duoc tinh toan theo % cua parent
                desireHeight = height * parentHeight;
            } else {
                desireHeight = height < parentHeight ? height
                        : parentHeight;
            }
        }
        // Cuoi cung, ta set kich thuoc cho view
        this.setMeasuredDimension((int) desireWidth, (int) desireHeight);
    }
};

【问题讨论】:

  • 不是重复的,该问题涉及静态 xml 布局而不是动态代码生成的布局。
  • 我认为你的重力没有问题。只需为你的布局设置一个大小,比如 match_parent.den 看看你的文本 z 是否在右边。

标签: android


【解决方案1】:

在 Java 中制作布局很有用,但有时重要的实现细节在您这样做时会隐藏起来。这是其中之一。

当您调用addView() 时,正在添加的视图会从父视图中获取一个默认的 LayoutParams 对象。对于 LinearLayout,此默认 LayoutParams 对象的宽度和高度指定为 WRAP_CONTENT。由于您的 TextView 仅与其内容一样大,因此更改其重力没有明显效果。

您需要创建一个 LayoutParams 对象并将其提供给addView()。您可以将其重力设置为RIGHT,也可以使用MATCH_PARENT 作为其宽度。

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, params);

请注意,LinearLayout 默认具有HORIZONTAL 方向,当它是屏幕布局的根时,它往往不是那么有用。你可能想打电话给linearLayout.setOrientation(LinearLayout.VERTICAL)

【讨论】:

  • 我知道,但我必须使用 onMeasure(),因为我想通过父尺寸的百分比来设置 TextView 的大小。LayoutParams 有 WEIGHT 属性,但它只是改变宽度(使用水平)或高度(垂直)
猜你喜欢
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 2013-01-04
  • 1970-01-01
  • 2011-11-24
  • 2021-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多