[英]Android Custom LinearLayout and inflating xml
I have an xml
layout that when inflated into a View
object, the layout displays as expected. 我有一个
xml
布局,当膨胀到View
对象时,布局按预期显示。 However, I created a custom LinearLayout
class, and inflate the xml
layout when creating an instance of the class by using 但是,我创建了一个自定义的
LinearLayout
类,并在使用时创建类的实例时膨胀xml
布局
LayoutInflater.from(context).inflate(R.layout.icon_item, this, true);
in the constructor. 在构造函数中。 I am not setting any
LayoutParams
or anything on the custom LinearLayout
class, and when I try to display the custom class, it seems there are some weird margins placed that cut off some of the text in the layout inside. 我没有在自定义
LinearLayout
类上设置任何LayoutParams
或任何东西,当我尝试显示自定义类时,似乎放置了一些奇怪的边距,切断了布局中的一些文本。
I want to know if there is any way to set the custom LinearLayout
so it has no effects on the layout xml, but just acts as an invisible container. 我想知道是否有任何方法来设置自定义
LinearLayout
因此它对布局xml没有影响,但只是作为一个不可见的容器。 I thought setting "attachToRoot" to true when inflating would solve this, but didn't work as expected. 我认为在充气时将“attachToRoot”设置为true会解决这个问题,但是没有按预期工作。
When using inflate(..)
with attachToRoot = true, this is the equivalent of physically placing the XML inside the ViewGroup like so: 当使用带有attachToRoot = true的inflate
inflate(..)
时,这相当于将XML物理放置在ViewGroup中,如下所示:
<LinearLayout ..>
<TextView .. />
</LinearLayout>
Also meaning that when being measure
'd and laid out by the LinearLayout
it will inherit any layout properties imposed in your xml layout you are inflating. 同样意味着当
measure
'd并由LinearLayout
布局时,它将继承您正在膨胀的xml布局中强加的任何布局属性。
So if you <TextView..>
contains android:layout_margin="8dp"
or padding
for example then your CustomLinearLayout
will also layout the view using these Layout
properties. 因此,如果您
<TextView..>
包含android:layout_margin="8dp"
或padding
,那么您的CustomLinearLayout
也将使用这些Layout
属性布局视图。
I would post more code if you want me to fix/create a better solution for you. 如果您希望我为您修复/创建更好的解决方案,我会发布更多代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.