单一视图布局文件:编译器是否与Autowrap布局/ ViewGroup中? [英] Single View Layout Files: Does Compiler Autowrap with Layout/ViewGroup?

查看:278
本文介绍了单一视图布局文件:编译器是否与Autowrap布局/ ViewGroup中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个只包含一个的TextView 布局文件,我没有问题从活动中夸大它

不过,如果我试图夸大了,而不是包含了一个自定义视图相似的布局文件,然后我得到的通货膨胀例外的。
我可以得到一个自定义视图在这种情况下膨胀的唯一方法是在布局/ ViewGroup中(即的LinearLayout )。

所以,我在想,如果编译器会自动包装一个内置的视图在布局/ ViewGroup中(即LinearLayout中),如果它检测
该视图尚未嵌套一个之内?

(我会测试这个,当我得到一些时间来建立一个模拟器,并提取布局树,后
任何结果。)

感谢您的帮助。


案例1:工程确定

  TextViewLayoutFile.axml< XML版本=1.0编码=UTF-8&GT?;
<的TextView
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@ + ID / tv_name
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT/>LayoutInflater infalInflater =(LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
鉴于= infalInflater.Inflate(Resource.Layout.TextViewLayoutFile,NULL);


案例2:如果MyTextView被包裹在一个布局(LinearLayout中)只适用

  2A:产生通货膨胀的异常
-------------------------------------------------- --------------------------
TextViewLayoutFile.axml< XML版本=1.0编码=UTF-8&GT?;
< AppName.Views.MyTextView
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@ + ID / tv_name
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =WRAP_CONTENT/>LayoutInflater infalInflater =(LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
鉴于= infalInflater.Inflate(Resource.Layout.TextViewLayoutFile,NULL);2B:膨胀确定
-------------------------------------------------- --------------------------
TextViewLayoutFile.axml< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@ + ID / ll_android_is
  机器人:layout_width =match_parent
  机器人:layout_height =WRAP_CONTENT>  < AppName.Views.MyTextView
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / tv_name
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT/>< / LinearLayout中>LayoutInflater infalInflater =(LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
鉴于= infalInflater.Inflate(Resource.Layout.TextViewLayoutFile,NULL);

其中,

 命名空间AppName.Views
{
    公共类MyTextView:TextView的
    {
        公共MyTextView(上下文的背景下):基地(上下文){}        公共MyTextView(上下文的背景下,IAttributeSet属性):基地(上下文,属性){}
    }
}


解决方案

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

 的TextView(上下文的背景下,ATTRS的AttributeSet,INT defStyle)

我已经previously有充气和不覆盖所有默认的构造函数实例化的东西,可能会增加燃料的火这里还有问题。至于自动包装的魔力。至于它会自动为您呢,我却无法说。

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) { }        
    }
}

解决方案

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.

这篇关于单一视图布局文件:编译器是否与Autowrap布局/ ViewGroup中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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