Single View Layout Files: Does Compiler Autowrap with Layout/ViewGroup?

感情迁移 提交于 2019-12-23 18:48:19

问题


If I have a layout file that simply contains a single TextView, I have no problems inflating it from within an activity.

However, if I try to inflate a similar layout file that instead contains a single custom view, then I get a inflation exception. The only way I can get a custom view to inflate in this case is to wrap it in within Layout/ViewGroup (ie, LinearLayout).

Therefore, I was wondering if the compiler will auto-wrap a built-in view in a Layout/ViewGroup (ie, LinearLayout) if it detects that the view is not already nested within one ?

(I'm going to test this when I get some time to setup an emulator and extract a layout tree, and post any findings.)

Thank you for the help.


Case 1: works ok

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);

Case 2: Only works if the MyTextView is wrapped in a layout (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);

where

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

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

回答1:


There is one more constructor I believe you have not provided,

TextView(Context context, AttributeSet attrs, int defStyle)

I've previously had issues inflating and instantiating things without covering all the default constructors, may be adding fuel to the fire here as well. As far as the magic of the auto-wrapping. As far as what it does automatically for you, I can't say however.



来源:https://stackoverflow.com/questions/15092785/single-view-layout-files-does-compiler-autowrap-with-layout-viewgroup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!