【发布时间】:2011-09-03 16:47:13
【问题描述】:
我正在尝试确定某些 UI 元素的实际尺寸(以像素为单位)!
这些元素是从 .xml 文件中扩展而来的,并使用倾斜宽度和高度进行初始化,以便 GUI 最终支持多种屏幕尺寸和 dpi (as recommended by android specs)。
<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 >
定义为在我的 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) 的报告信息; 以下是: 密度 = 1,5 密度Dpi = 240 宽度像素 = 600 高度像素 = 1024
现在,我知道 android 的规则是:pixel = dip * (dpi /160)。但是返回的值没有任何意义。对于 (90dip X 110dip) 的 ImageView,measure() 方法的返回值是 (270 x 218),我假设它以像素为单位!
有人知道为什么吗? 返回的值是像素吗?
顺便说一句:我一直在测试相同的代码,但使用的是 TextView 而不是 ImageView,一切似乎都运行良好!为什么!?!?
【问题讨论】:
标签: android android-layout android-widget android-ui android-xml