3

レイアウト ファイルに単一TextViewactivity.

ただし、代わりに 1 つのカスタム ビューを含む同様のレイアウト ファイルをインフレートしようとすると、インフレ例外が発生します。この場合、カスタム ビューを膨張させる唯一の方法は、Layout/ViewGroup(つまり、LinearLayout) 内にラップすることです。

Layout/ViewGroupしたがって、ビューがまだ1つにネストされていないことをコンパイラが検出した場合、コンパイラが組み込みビューを(つまり、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) { }        
    }
}
4

1 に答える 1