【问题标题】:Android HorizontalScrollView Child WidthAndroid Horizo​​ntalScrollView 子宽度
【发布时间】:2012-10-04 02:24:07
【问题描述】:

我有一个 Horizo​​ntalScrollView,其中包含一个包含一些自定义视图的 LinearLayout。现在,我通过这一行(在孩子的构造函数中)固定孩子的大小:

setLayoutParams(new LayoutParams(200, LayoutParams.FILL_PARENT));

我想做的是将组件的宽度设置为自身高度的 30%,而不是实际的 200px。但我仍然需要“FILL_PARENT”的高度。

当我覆盖 onMeasure 方法时,我尝试了:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    System.out.println(widthMeasureSpec + "/" + heightMeasureSpec);
}

但我得到了输出:

1073742024/1073742586

所以看起来我在这里做不了很多事情,但我仍然没有很多信息:)

知道怎么做吗?

我确切地说我正在模拟器上进行测试(不知道这是否重要)。

【问题讨论】:

  • 这应该足够了:@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int height = getMeasuredHeight(); super.onMeasure(MeasureSpec.makeMeasureSpec(height / 3, MeasureSpec.EXACTLY), heightMeasureSpec); }.

标签: android width children horizontalscrollview


【解决方案1】:

你可以做一些事情,比如获取屏幕的尺寸

DisplayMetrics display = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics( display );
int screenW = display.widthPixels;
int screenH = display.heightPixels;

如果你想要去掉 0.3 的组件的高度是整个屏幕的高度,那么就这样做吧:

setLayoutParams(new LayoutParams((int)(screenH*0.3), LayoutParams.FILL_PARENT));

我可能误解了你的问题,我刚醒来 :) 让我知道它是否有效或者你是否正在寻找其他东西

【讨论】:

  • 问题在于它是基于屏幕的大小而不是父视图的大小。
  • 哦,所以父View不是屏幕的整个大小?