【问题标题】:Align a Button Text after Repositioning the button onLayout in Android在Android中重新定位按钮onLayout后对齐按钮文本
【发布时间】:2013-06-21 06:59:52
【问题描述】:

当我们从 onLayout 重新定位 Button 时如何对齐 Button 文本?我有一个图表,它有许多水平轴,每个轴都有三个按以下顺序排列的按钮

现在我必须重新定位,以便图表上的所有按钮正确对齐并放置在我需要的位置。除了蓝色按钮的文本对齐之外,一切都很好。如果我说重力为中心,它将高度假定为蓝色和红色按钮的组合高度,并将文本一半放在红色按钮后面。如果没有说它在黑色区域,我们在按钮的左端只有蓝色的小箭头,但我希望文本位于带有重力底部和 cent_horizo​​ntal 的蓝色区域。有趣的是我的重力底部和中心水平适用于平板电脑。

这是我的 OnLayout 代码:

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);


    //this.strikeButton.layout(left,top,right,bottom);
    int height = this.getMeasuredHeight();
    int width = this.getMeasuredWidth();
    int buttonWidth = this.upArrowButton.getMeasuredWidth();
    int buttonHeight = (2*height)/5;
    int right = width - buttonWidth;

    int left = 0;
    int top = height / 2;
    top -= buttonHeight / 2;

    int bottom = top + buttonHeight;
    this.strikePriceButton.layout(left, top, right, bottom);
    this.upArrowButton.layout(left, 0, right, height / 2);
    this.downArrowButton.layout(left, height / 2, right, height);
    this.upArrowButton.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL);
    this.downArrowButton.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL);
    this.strikePriceButton.setGravity(Gravity.CENTER);
    }

这是我的 OnMeasure 函数:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    this.setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), App.isTablet(context) ? 96 : 128);
}

我尝试为平板电脑和手机使用相同的高度,但其中一个看起来不太好,所以我不得不使用不同的高度。

【问题讨论】:

    标签: android android-layout button text-align


    【解决方案1】:

    只需要使用 dp 而不是使用高度和宽度值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-20
      • 2011-04-07
      • 1970-01-01
      • 2012-07-05
      • 2011-05-10
      • 1970-01-01
      • 2014-11-21
      • 1970-01-01
      相关资源
      最近更新 更多