我正在尝试确定一些UI元素的实际像素尺寸!
这些元素是从一个.xml文件中膨胀出来的,并且用dip宽度和高度初始化,以便GUI最终支持多个屏幕尺寸和dpi(按照Android规范建议)。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="150dip"
android:orientation="vertical">
<ImageView
android:id="@+id/TlFrame"
android:layout_width="110dip"
android:layout_height="90dip"
android:src="@drawable/timeline_nodrawing"
android:layout_margin="0dip"
android:padding="0dip"/></LinearLayout>
上面的XML表示一个框架。但我会在这里描述的水平布局中动态添加许多框架。
<HorizontalScrollView
android:id="@+id/TlScroller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="0dip"
android:padding="0dip"
android:scrollbars="none"
android:fillViewport="false"
android:scrollbarFadeDuration="0"
android:scrollbarDefaultDelayBeforeFade="0"
android:fadingEdgeLength="0dip"
android:scaleType="centerInside">
<!-- HorizontalScrollView can only host one direct child -->
<LinearLayout
android:id="@+id/TimelineContent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_margin="0dip"
android:padding="0dip"
android:scaleType="centerInside"/>
</HorizontalScrollView >
我定义了一个添加一个帧(Frame)的方法在我的Java代码中:
private void addNewFrame()
{
LayoutInflater inflater = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
TextView frameNumber = (TextView) root.findViewById(R.id.FrameNumber);
Integer val = new Integer(_nFramesDisplayed+1); //+1 to display ids starting from one on the user side
frameNumber.setText(val.toString());
++_nFramesDisplayed;
_content.addView(root);
// _content variable is initialized like this in c_tor
// _content = (LinearLayout) _parent.findViewById(R.id.TimelineContent);
}
然后在我的代码中,我尝试获取实际的像素大小,因为我需要这个来绘制一些OpenGL内容。
LayoutInflater inflater = (LayoutInflater) _parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.tl_frame, null);
ImageView frame = (ImageView) root.findViewById(R.id.TlFrame);
frame.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
frame.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
final int w = frame.getMeasuredWidth();
final int h = frame.getMeasuredHeight();
除了这些值比ImageView实际像素大小大得多之外,一切似乎都正常。
从getWindowManager().getDefaultDisplay().getMetrics(metrics)获取的报告信息如下: density = 1.5 densityDpi = 240 widthPixel = 600 heightPixel = 1024
现在,我知道Android的规则是:pixel = dip *(dpi / 160)。但返回的值毫无意义。对于(90dip X 110dip)的ImageView,measure()方法的返回值为(270 x 218),我假设它是以像素为单位的!
有人知道为什么吗? 返回的值是否以像素为单位?
顺便说一句:我已经使用TextView而不是ImageView测试了相同的代码,一切似乎都正常!为什么???
MeasureSpec.UNSPECIFIED
。这会导致水平方向出现无限滚动,并使文本无法自动换行。你需要将垂直维度设置为MeasureSpec.UNSPECIFIED
,水平维度设置为最大宽度,以触发自动换行并获得准确的高度测量值。记得根据需要考虑内边距。 - Bob Aman