我有一个视图扩展AdapterView并使用ArrayAdapter设置数据 这是我的ArrayAdapter
ArrayAdapter.createFromResource(this, string_array, R.layout.action_bar_simple_spinner_item);
我的布局中的textview,但是gravity属性确实有效。我的textview中没有文字显示。但如果我删除属性,则会显示文字。
<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
答案 0 :(得分:0)
您需要layout_gravity
,而不是gravity
layout_gravity
指的是View
与其包含ViewGroup
的关系,而gravity
适用于该视图的内容。
除此之外,您还可以继续使用gravity
,但将layout_width
的{{1}}设置为TextView
。要确定什么是最佳解决方案,我需要查看更多布局XML