意外的LayoutParams与膨胀的LinearLayout [英] Unexpected LayoutParams with an inflated LinearLayout

查看:160
本文介绍了意外的LayoutParams与膨胀的LinearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我将用的addChild更大的容器视图视图的XML定义。它是基于一个的LinearLayout 键,基本上是这样的:

I have an XML definition for a view that I am adding to a larger container view with addChild. It's based on a LinearLayout and looks basically like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="52dip"
android:background="@drawable/box_bg"
android:clickable="true"
android:onClick="onTreeBoxClick"
android:orientation="horizontal" >

<ImageView android:id="@+id/box_photo"
    android:layout_width="45dip"
    android:layout_height="45dip"
        ...
/>
<RelativeLayout
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="wrap_content"
>

(余略 - 可能不相关的,因为它基本上按设计工作)

(Remainder omitted -- probably not relevant since it's basically working as designed)

当我创建这些意见,我已经发现,看上去有些奇怪,我的下列行为:

When I create these views, I have found the following behaviors that seem odd to me:


  1. 右键后,我膨胀的观点,getLayoutParameters()返回null。

  1. Right after I inflate the view, getLayoutParameters() returns null.

在我打电话的addChild()将其添​​加到它的父,getLayoutParameters()返回一个有效的对象。

After I call addChild() to add it to its parent, getLayoutParameters() returns a valid object.

审视LayoutParameters,我发现无论宽度和高度设置为-2(WRAP_CONTENT),这显然不是我的XML文件中指定。

Examining the LayoutParameters, I find both width and height set to -2 (WRAP_CONTENT), which is clearly not what I specified in the XML file.

当我看着封闭的ImageView的布局参数,它在指定的值读出。

When I look at the layout parameters of the enclosed ImageView, it reads out at the specified values.

谁能解释一下是怎么回事?为什么不是我规定的高度被发现的?

Can anyone explain what is going on here? Why isn't my specified height being noticed?

这是不是真的影响到我,因为父视图是一个自定义视图,其中,我强迫与MeasureSpec等孩子们的最终尺寸,但我想无论如何要明白这一点!

This isn't really affecting me since the parent view is a custom view wherein I force the final dimensions of the children with MeasureSpec etc., but I'd like to understand this anyway!

推荐答案

您没有提供一些细节是很重要的。

You didn't provide some details which are important.

1),紧接着我膨胀的观点,getLayoutParameters()返回null。

1) Right after I inflate the view, getLayoutParameters() returns null.

我会假设你使用这样的:

I would assume that you used this:

inflater.inflate(R.layout.content, null);

在这种情况下, LayoutInflater 不能使(全部)适当的LayoutParams 的LinearLayout ,因为它不知道谁将会是它的父(所以它可以创建正确的类型的LayoutParams )的。如果你会使用这样的:

in this case the LayoutInflater can't make(at all) proper LayoutParams for the root Linearlayout because it doesn't know who is going to be its parent(so it can create the right type of LayoutParams). If you would use this:

inflater.inflate(R.layout.content, someOtherLayout, false/true);

然后是根的LinearLayout 将有适当的的LayoutParams ,因为它会看到的类型someOtherLayout ,并从该信息创建的LayoutParams 。您可能需要提供code的片段,以获得更好的答案,如果这是你现在做什么。

then the root LinearLayout will have proper LayoutParams because it will see the type of someOtherLayout and create the LayoutParams from this information. You may want to provide a snippet of code to get a better answer if this is not what you currently do.

2)我叫的addChild()后,将其添加到其母公司,
  getLayoutParameters()返回一个有效的对象。

2) After I call addChild() to add it to its parent, getLayoutParameters() returns a valid object.

我假设你谈谈 addView()方法。在 addView()方法将检查的LayoutParams 这是试图添加视图,如果这些的LayoutParams 然后它会自动分配视图中的的LayoutParams 对象由归国其 generateDefaultLayoutParams()方法。

I assume that you speak about the addView() method. The addView() method will check the LayoutParams of the view which is trying to add and if those LayoutParams are null then it will automatically assign that view a LayoutParams object returned by its generateDefaultLayoutParams() method.

3)审查LayoutParameters,我发现无论宽度和高度设置为
  -2(WRAP_CONTENT),这显然不是我的XML文件中指定。

3) Examining the LayoutParameters, I find both width and height set to -2 (WRAP_CONTENT), which is clearly not what I specified in the XML file.

正如我在2日表示,生成的的LayoutParams generateDefaultLayoutParams来了()方法,它会返回一个作为父的目的是做的LayoutParams 实例。例如,的LinearLayout 与方向横向(默认的)将返回一个的LayoutParams 实例设置为 WRAP_CONTENT 。

As I said at 2, the generated LayoutParams are coming from the generateDefaultLayoutParams() method which will return a LayoutParams instance as the parent was designed to do. For example, a LinearLayout with orientation HORIZONTAL(the default one) will return a LayoutParams instance with width/height set to WRAP_CONTENT.

4)当我看到封闭的ImageView的布局参数,它
  在指定的值读出。

4) When I look at the layout parameters of the enclosed ImageView, it reads out at the specified values.

由于该 LayoutInflater 照顾了这一点,因为在的ImageView 它在布局的内部,有一个已知父从中 LayoutInflater 可以作出适当的的LayoutParams

Because the LayoutInflater took care of this, as the ImageView it's in the interior of the layout and has a known parent from which the LayoutInflater can make the proper LayoutParams.

这篇关于意外的LayoutParams与膨胀的LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆