【问题标题】:TextView gravity does not work in my custom AdapterViewTextView 重力在我的自定义 AdapterView 中不起作用
【发布时间】:2017-01-19 02:36:40
【问题描述】:

我有一个视图扩展了 AdapterView 并使用 ArrayAdapter 设置数据。
这是我的 ArrayAdapter

ArrayAdapter.createFromResource(this, string_array, R.layout.action_bar_simple_spinner_item);

我的布局中有一个文本视图,但重力属性确实有效。我的文本视图中没有文本显示。但是,如果我删除属性,则会显示文本。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/text1"
      style="?android:attr/spinnerItemStyle"
      android:singleLine="true"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="123"
      android:textSize="16dip"
      android:textColor="@color/black"
      android:gravity="center"/>

在我的 AdapterView 中,我覆盖了 onMeasure 和 onLayout。这是我的代码:

@Override
protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {
    final int size = getChildCount();

    int height = getHeight();
    int count = getChildCount();

    Log.d(TAG, "measureChildren count: " + count);
    if (count == 0) {
        return;
    }
    ...
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        measureChild(child, MeasureSpec.makeMeasureSpec(itemWidth, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
    }
    invalidate();
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    Log.d(TAG, "onMeasure widthSize: " + widthSize + " heightSize: " + heightSize);
    setMeasuredDimension(widthSize, heightSize);
    measureChildren(widthSize, heightSize);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
    removeAllViewsInLayout();
    int size = mAdapter != null ? mAdapter.getCount() : 0;
    Log.d(TAG, "onLayout size: " + size);
    Log.d(TAG, "l: " + l + " t: " + t + " r: " + r + " b: " + b);
    if (size == 0) return;
    ...
    int left = l;
    for (int i = 0; i < size; i++) {
        Log.d(TAG, "left: " + left);
        View child = obtainView(i);

        child.layout(left, 0, left + itemWidth, mHeight);
        addViewInLayout(child, i, i < mLp, false);
        left += itemWidth;
    }
    invalidate();
}

@Override
public void setAdapter(Adapter adapter) {
    mAdapter = adapter;
    if (mAdapter.getCount() > 0) {
        if (mSelectedPosition == INVALID_POSITION) {
            mSelectedPosition = 0;
        }
    }

    int size = mAdapter != null ? mAdapter.getCount() : 0;
    if (size == 0) return;
    ...
    mLp.width = itemWidth;
    Log.d(TAG, "setAdapter size: " + size);
    requestLayout();
    invalidate();
}

一些日志:

01-19 10:16:55.280 D/MyView(30120): setAdapter size: 3
01-19 10:16:55.320 D/MyView(30120): onMeasure widthSize: 1080 heightSize: 128
01-19 10:16:55.320 D/MyView(30120): measureChildren count: 0
01-19 10:16:55.340 D/MyView(30120): onMeasure widthSize: 1080 heightSize: 128
01-19 10:16:55.340 D/MyView(30120): measureChildren count: 0
01-19 10:16:55.341 D/MyView(30120): onLayout size: 3
01-19 10:16:55.341 D/MyView(30120): l: 0 t: 168 r: 1080 b: 296
01-19 10:16:55.342 D/MyView(30120): left: 0
01-19 10:16:55.342 D/MyView(30120): left: 360
01-19 10:16:55.343 D/MyView(30120): left: 720
01-19 10:16:55.350 D/MyView(30120): onDraw

【问题讨论】:

    标签: android textview viewgroup


    【解决方案1】:

    你想要layout_gravity,而不是gravity

    layout_gravity 指的是 View 与其包含的 ViewGroup 的关系,而 gravity 则适用于该视图的内容。

    按照这些思路,您也可以继续使用gravity,但将TextViewlayout_width 设置为match_parent。为了确定什么是最佳解决方案,我需要查看更多您的布局 XML

    【讨论】:

    • 我希望文本显示在文本视图的中心,而不是文本视图显示在我的视图组的中心。
    • 在这种情况下,我提供的第二种/替代解决方案可以解决问题。通过使用换行内容的宽度,您不会给视图任何额外的空间来使文本居中。视图的边界是围绕文本“收缩包装”的。一旦您将视图更改为更宽(使用 match_parent、layout_weight 或特定宽度测量),您将看到文本保持居中。
    • 我用 MeasureSpec.EXACTLY 测量孩子。我仍然用 match_parent 替换 wrap_content,但 textview 中仍然没有文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多