lwx635991604 2016-09-09 03:50 采纳率: 100%
浏览 989
已采纳

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条回答 默认 最新

  • 脸萌王教授 2016-09-09 06:42
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误