2 lwx635991604 lwx635991604 于 2016.09.09 11:50 提问

ViewGroup中添加了HorizontalScrollView是无法显示?

public class CustomeTable1 extends ViewGroup {
private static int DATE_TXT_COLOR = Color.WHITE;
private static int DATE_BG_COLOR1 = Color.GRAY;
private static int DATE_BG_COLOR2 = Color.DKGRAY;
private static int DATE_TXT_SIZE = 30;

private int dateTxtColor;
private int dateBgColor1;
private int dateBgColro2;
private int dateTetSize;

private static int lotteryNum = 15;
private static int dateNum = 100;
private int screenDateNum = 0;
private int mDateW = 150;
private int mDateH = 50;
/**
 * 彩票号码
 */
private int mLotteryW = 50;
private int mLotteryH = 50;
/**
 * 彩票号码格子宽度
 */
private int lotteryW = 0;
/**
 * 彩票号码格子高度
 */
private int lotteryH = 0;

private CustomTableTitle mCustomTableTitle;
private HorizontalScrollView mHSV;

public CustomeTable1(Context context) {
    this(context, null);
}

public CustomeTable1(Context context, AttributeSet attrs) {
    this(context, attrs, 0);

}

public CustomeTable1(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initViews(attrs);
}

private void initViews(AttributeSet attrs) {
    TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.custom_table);
    dateTxtColor = ta.getColor(R.styleable.custom_table_date_text_color, DATE_TXT_COLOR);
    dateBgColor1 = ta.getColor(R.styleable.custom_table_date_bg_color1, DATE_BG_COLOR1);
    dateBgColro2 = ta.getColor(R.styleable.custom_table_date_bg_color2, DATE_BG_COLOR2);
    dateTetSize = ta.getColor(R.styleable.custom_table_date_text_size, DATE_TXT_SIZE);
    ta.recycle();

    mDateW = getRect("00000000", dateTetSize).width() + 20;
    mDateH = getRect("00000000", dateTetSize).height() + 20;
    lotteryW = (getDisplayMetrics().widthPixels - mDateW) / (lotteryNum > 10 ? 10 : lotteryNum);
    lotteryH = lotteryW;
    mDateW = getDisplayMetrics().widthPixels - lotteryW * (lotteryNum > 10 ? 10 : lotteryNum);

    mCustomTableTitle = new CustomTableTitle(getContext());
    try {
        mHSV = new HorizontalScrollView(getContext());
        mHSV.setFillViewport(true);
        mHSV.addView(mCustomTableTitle, 0);
        addView(mHSV);
    } catch (Exception e) {
        e.printStackTrace();
    }

// addView(mCustomTableTitle);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    mHSV.layout(mDateW, 0, mDateW + 10 * lotteryW, lotteryH);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    setMeasuredDimension(mDateW + lotteryNum * lotteryW, lotteryH * 101);
}


public DisplayMetrics getDisplayMetrics() {
    WindowManager wm = (WindowManager) getContext()
            .getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics metric = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metric);

// int width = metric.widthPixels; // 屏幕宽度(像素)
// int height = metric.heightPixels; // 屏幕高度(像素)
// float density = metric.density; // 屏幕密度(0.75 / 1.0 / 1.5)
// int densityDpi = metric.densityDpi; // 屏幕密度DPI(120 / 160 / 240)
return metric;
}

public Rect getRect(String str, int textSize) {
    Paint pFont = new Paint();
    pFont.setTextSize(textSize);
    Rect rect = new Rect();
    pFont.getTextBounds(str, 0, str.length(), rect);
    return rect;
}

}

1个回答

professor_wang_tcl
professor_wang_tcl   2016.09.09 14:42
已采纳

在onMeasure中别调super的onMeasure方法了,主动调mHSV的measure方法,
mHSV.measure(MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY),MeasureSpec.makeMeasureSpec(500, MeasureSpec.EXACTLY));

lwx635991604
lwx635991604 谢谢,帮我大忙了!
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!