简体   繁体   English

Android自定义LinearLayout和膨胀xml

[英]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.

相关问题 将布局XML扩展为自定义LinearLayout会创建冗余的LinearLayout视图 - Inflating Layout XML to custom LinearLayout creates redundant LinearLayout view 动态膨胀LinearLayout-Android - Inflating a LinearLayout dynamically - Android 自定义LinearLayout Binary XML文件行#1:错误夸大类 - custom LinearLayout Binary XML file line #1: Error inflating class Android:自定义控件/将XML扩展到布局上? - Android: Custom Controls/Inflating an XML onto a layout? Android在XML ClassNotFound中夸大自定义视图错误 - Android inflating custom view in XML ClassNotFound error Android自定义视图与膨胀XML - Android Custom Views vs inflating xml Android - 在XML中膨胀自定义View时出错 - Android - Error when inflating custom View in XML 将自定义 LinearLayout 膨胀为扩展 LinearLayout 的自定义 class 时出现问题 - Problems inflating a custom LinearLayout into a custom class that extends LinearLayout 二进制 XML 文件第 2 行:膨胀 class android.widget.LinearLayout 时出错 - Binary XML file line #2: Error inflating class android.widget.LinearLayout MapView:android.view.InflateException:二进制XML文件第2行:膨胀类linearLayout时出错 - MapView :android.view.InflateException: Binary XML file line #2: Error inflating class linearLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM