设置权重时如何 getMeasuredWidth()
我做了一个简单的例子来说明这个问题。这是一个简单的 LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
然后我在 onCreate() 中填充它
private final String TAG = getClass().getSimpleName();
ImageView iv1;
ImageView iv2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout container = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight=1.0f;
iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.icon);
iv2 = new ImageView(this);
iv2.setImageResource(R.drawable.icon);
container.addView(iv1, lp);
container.addView(iv2, lp);
container.measure(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
Log.d(TAG, " measured width(1): " + iv1.getMeasuredWidth());
Log.d(TAG, " measured width(2): " + iv2.getMeasuredWidth());
,其中 icon 是 48x48dp 的简单图像。问题是:为什么 getMeasuredWidth 在日志中返回 48,但是当我在层次结构查看器中查看时,mMeasureWidth 是 160?换句话说,真正的问题是..当视图在 LinearLayout 中拉伸时,如何获得视图的真实宽度/高度(在 Java 中)?调用 requestLayout()、measure() 不起作用。我可以尝试获取权重总和并计算 LinearLayout 所做的一切,但也许有一个更优雅的解决方案。
I have made a simple example to ilustrate the problem. Here is a simple LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
and then I populate it in onCreate()
private final String TAG = getClass().getSimpleName();
ImageView iv1;
ImageView iv2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout container = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.weight=1.0f;
iv1 = new ImageView(this);
iv1.setImageResource(R.drawable.icon);
iv2 = new ImageView(this);
iv2.setImageResource(R.drawable.icon);
container.addView(iv1, lp);
container.addView(iv2, lp);
container.measure(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
Log.d(TAG, " measured width(1): " + iv1.getMeasuredWidth());
Log.d(TAG, " measured width(2): " + iv2.getMeasuredWidth());
where icon is a simple image of 48x48dp. question is: why getMeasuredWidth returns 48 in Logs, but when i look in hierarchy viewer mMeasureWidth is 160? In other words the real question is .. how can I get real width/height (in Java) of the view when it is stretched in a LinearLayout ? Calling requestLayout(), measure() does not work. I could try to get a sum of weigths and calculate everything that LinearLayout does, but maybe there is a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
onCreate 在显示 Activity 之前调用。
因此,iv1.getMeasuredWidth() 还不是您期望的值,应该稍后在另一个方法中调用。我认为 onWindowFocusChange 是正确的。
onCreate is called much before the activity is displayed.
Therefore, iv1.getMeasuredWidth() is not yet the value you expect and should be called in another method, later. I think onWindowFocusChange is correct.