【问题标题】:Single View Layout Files: Does Compiler Autowrap with Layout/ViewGroup?单视图布局文件:编译器是否使用布局/视图组自动换行?
【发布时间】:2013-02-12 02:53:31
【问题描述】:

如果我有一个仅包含单个 TextView 的布局文件,那么从 activity 中扩展它就没有问题。

但是,如果我尝试扩充包含单个自定义视图的类似布局文件,则会收到 扩充异常。 在这种情况下,我可以让自定义视图膨胀的唯一方法是将其包装在Layout/ViewGroup(即LinearLayout)中。

因此,我想知道如果编译器检测到,它是否会在 Layout/ViewGroup(即 LinearLayout)中自动包装内置视图 视图还没有嵌套在一个视图中?

(当我有时间设置模拟器并提取布局树并发布时,我将对此进行测试 任何发现。)

感谢您的帮助。


案例 1:工作正常

TextViewLayoutFile.axml

<?xml version="1.0" encoding="utf-8"?>
<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tv_name"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>

LayoutInflater infalInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = infalInflater.Inflate(Resource.Layout.TextViewLayoutFile, null);

案例 2:仅当 MyTextView 包含在布局中时才有效(LinearLayout)

2a: Produces inflation exception
----------------------------------------------------------------------------
TextViewLayoutFile.axml

<?xml version="1.0" encoding="utf-8"?>
<AppName.Views.MyTextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tv_name"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>

LayoutInflater infalInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = infalInflater.Inflate(Resource.Layout.TextViewLayoutFile, null);

2b: Inflates ok
----------------------------------------------------------------------------
TextViewLayoutFile.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ll_android_is"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <AppName.Views.MyTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

LayoutInflater infalInflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
view = infalInflater.Inflate(Resource.Layout.TextViewLayoutFile, null);

在哪里

namespace AppName.Views 
{
    public class MyTextView : TextView
    {
        public MyTextView(Context context) : base(context) { }

        public MyTextView(Context context, IAttributeSet attributes) : base(context, attributes) { }        
    }
}

【问题讨论】:

    标签: c# android android-layout xamarin.android


    【解决方案1】:

    还有一个构造函数我相信你没有提供,

    TextView(Context context, AttributeSet attrs, int defStyle)
    

    我之前在没有覆盖所有默认构造函数的情况下膨胀和实例化事物时遇到过问题,这也可能是火上浇油。至于自动包装的魔力。至于它自动为你做什么,我不能说。

    【讨论】:

    • 构造函数所做的只是传入第三个参数,即正在使用的样式,以防您想要指定默认/父/应用程序样式以外的其他内容(我认为样式是基于视图的,并且主题是基于应用程序的,或类似的)。
    • 为了完整起见,从代码实例化视图时需要第一个构造函数;从 xml 布局文件中扩展视图时的第二个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多