什么是机器人之间的区别:高度和android:layout_height? [英] What's the difference between android:height and android:layout_height?

查看:109
本文介绍了什么是机器人之间的区别:高度和android:layout_height?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <的TextView
    机器人:layout_width =match_parent
    机器人:layout_height =48dp
    机器人:paddingTop =48dp
    机器人:文本=测试
    />
 

我期望的TextView拥有的96dp一个测量高度,不管多大的空间(高度方向)测试了起来(希望它切断)。

相反,我得到了全面测试+ 48dp为空白。

我可以把任何有效的值 layout_height ,并得到同样的结果。

要得到我的96dp高度,我需要:

 <的TextView
    机器人:layout_width =match_parent
    机器人:layout_height =<任何有效的值GT;
    机器人:身高=48dp
    机器人:paddingTop =48dp
    机器人:文本=测试
    />
 

在这里再次,我可以把 layout_height 作为任何有效的值(但它需要有一个必需的属性)。

我从来没有使用XML中的高度属性,但我无法找到的文档的不同,特别是如何每一个计算,当元素它的操作上也有填充和/或保证金设定值。

是否有可用它商量,还是有人可以解释一下吗?任何资源

  • <一个href="http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#attr_android%3alayout_height">android:layout_height
  • <一个href="http://developer.android.com/reference/android/widget/TextView.html#attr_android%3aheight">android:height
  • <一个href="http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#MATCH_PARENT">LayoutParams

修改(问题总结):

这可能是我在用它的情况下,这是引起问题,所以我的从上面的例子可以忽略(我想在一个新的项目,并得到了不同的结果,从属性的组合)。

但我原来的问题仍然适用(部分重叠):

  1. 什么是它们之间的区别
  2. 当人应该使用比其他?
  3. 并使用一个影响使用其他的?
  4. 确实使用一个比其他属性的影响可控制视图的测量的尺寸,如填充或保证金?

编辑2(旁白):

在情况下,它可以帮助理解我的初步情况,与新鲜的项目进行比较:

我有设置一个活动 windowActionBarOverlay 为真,所以我得到我的内容下(半透明)流动的动作条。

我也用蒜香,现在是出现在动作条。本·魏斯建议我附上Crouton到不同的ViewGroup,但我不知道它的ViewGroup将其附加到。

相反,我提供的定制视图(其中在需要时,被充气)作为Crouton(从问题的第一部分的TextView)具有顶部填充等于动作条,所以现在它显示垂直下方在动作条为正常(与其他问题的另一天)。

解决方案

问题1:

开始»layout_«所有的属性都提示为的ViewGroup 中的的View 是的一部分。对于这个每个 android.view.ViewGroup 有一组<一href="http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html">LayoutParams.小部件本身并不使用它们。

机器人:身高参数是的初始的大小的 android.view.View 。但是,如果视图是部分的ViewGroup 的ViewGroup 将根据其布局规则调整的客体。

BTW:较新的API有一个新的安卓了minHeight 安卓了maxHeight 属性澄清程序员的意图<。 / P>

请注意,如果您没有设置机器人:身高视图将计算本身的尺寸。这是正确的事情的大部分时间。

问题2:

的ViewGroup 是抽象的 - 在这个问题的答案取决于其实际的子类,你使用的是

注意 WRAP_CONTENT 告诉使用的ViewGroup 使用机器人:身高。这意味着,这是在实施例中使用的正确的参数。

问题3:

是 - 和递归。 TableLayout 将读取和写入机器人:宽度属性,以找出最广泛的对象,并调整所有其他对象了。

问题4:

没有了安卓填充惟命是从的安卓layout_margin 。需要注意的是利润率的布局参数,而不是每一个布局管理器支持的利润率。

请参阅<一href="http://stackoverflow.com/questions/4619899/difference-between-a-views-padding-and-margin">margin VS填充获取详细信息。

最后的话:

这是比较容易考虑所有width和height属性是提示只。一个物体的大小说了算具有使用的ViewGroup 的布局管理器。

<TextView
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:paddingTop="48dp"
    android:text="Test"
    />

I would expect the TextView to have a measured height of 96dp, regardless of how much space (height-wise) "Test" took up (would expect it to cut off).

Instead, I get the full "Test" + 48dp for the padding.

I can put any valid value for layout_height and get the same result.

To get my 96dp height, I need:

<TextView
    android:layout_width="match_parent"
    android:layout_height="<any valid value>"
    android:height="48dp"
    android:paddingTop="48dp"
    android:text="Test"
    />

where again, I can put layout_height as any valid value (but it needs to be there as a required attribute).

I've never used the height attribute in XML but I couldn't find the difference in the docs, particularly how each one is calculated when the element it's operating on also has values set for padding and/or margin.

Are there any resources available which discuss it, or can someone explain it here?


Edit (summary of questions):

It might be the situation I used it in, which was causing an issue, so my examples from above can be ignored (I tried in a fresh project and got different results from the combinations of attributes).

But my original questions still apply (some overlaps):

  1. what's the difference between them?
  2. when should one be used over the other?
  3. does the use of one affect the use of the other?
  4. does the use of one have implications over other attributes which can control the measured dimensions of a view, like padding or margin?


Edit 2 (an aside):

In case it helps understand my initial situation as compared with the fresh project:

I have an activity which sets windowActionBarOverlay as true, so I get my content flowing under the (translucent) ActionBar.

I also use Croutons, which now were appearing under the ActionBar. Ben Weiss suggested I attach the Crouton to a different ViewGroup, but I wasn't sure which ViewGroup to attach it to.

Instead, I supplied a custom view (which is inflated when it is required) as the Crouton (the TextView from the first part of the question) with a top padding equal to that of the ActionBar, so now it displays vertically below the ActionBar as normal (with other issues for another day).

解决方案

Question 1:

All attributes starting with »layout_« are hints for the ViewGroup the View is part of. For this each android.view.ViewGroup has a set of LayoutParams. The widget itself does not use them.

The android:height parameter is the initial size of the android.view.View. But if the View is part ViewGroup then the ViewGroup will resize the objet according to its layout rules.

BTW: newer APIs have a new android:minHeight and android:maxHeight attribute to clarify programmers intent.

Note that if you don't set android:height the view will calculate the size itself. Which is right thing to do most of the time.

Question 2:

ViewGroup is abstract — the answer to this question depends which actual child class you are using.

Note that WRAP_CONTENT tells the to use ViewGroup to use android:height. Which means that this is the correct parameter to be used in your example.

Question 3:

Yes — and recursively . TableLayout will read and write the android:width attribute to find out the widest object and resized all other object to it.

Question 4:

No for android:padding but yes for android:layout_margin. Note that margins are a layout parameter and not every layout manager supports margins.

See margin vs padding for details.

Final words:

It is easier to consider all width and height attributes to be hints only. The final say of the size of an object has the layout manager of the used ViewGroup.

这篇关于什么是机器人之间的区别:高度和android:layout_height?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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