【发布时间】: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