samus
samus

Reputation: 6202

Inflating Custom Views - Layout File Inconsistencies

No, no one answered that question, and the problem still remains... This question here is about another symptom to the same problem (please see comments below):


In Monodroid atleast, when inflating a custom view from a layout, sometimes it needs to be wrapped in a ViewGroup (ie, LinearLayout) in order to not get an exception, and other times does not.

I was wondering if anyone is familiar with this situation, and if it happens in "raw" Android as well (ie, no Monodroid) ?

I always first try without, as in

TextView1.axml

<?xml version="1.0" encoding="utf-8"?>
<Monodroid.Activity1.TextView1
  android:id="@+id/text_view1"
  android:layout_width="300dp"
  android:layout_height="50dp"/>

but if I get an inflation exception, then I'll have to wrap it up

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

  <Monodroid.Activity1.TextView1
    android:id="@+id/text_view1"
    android:layout_width="300dp"
    android:layout_height="50dp"/>

</LinearLayout>

where

public class TextView1 : TextView
{
    public TextView1 (Context context) : base(context) { }
    public TextView1 (Context context, IAttributeSet attributes) : base(context, attributes) { }
    public TextView1 (Context context, IAttributeSet attributes, int defStyle) : base(context, attributes, defStyle) { }
}

Thank you.


This layout file inflates with no containing viewgroup:

<?xml version="1.0" encoding="utf-8"?>
<fieldinspection.droid.views.custom.FieldInput
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/RecordDataFieldInput"
  style="@style/FieldInput"
  android:layout_marginRight="0dip"/>

and this one (inner class PagedFragmentFieldInput) does not (it needs to be within a LinearLayout or else inflation exception):

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

  <FieldInspection.Droid.Views.ComplaintView.PagedFragmentRecordDataFieldBox.PagedFragmentFieldInput
    android:id="@+id/RecordDataFieldInput"
    style="@style/FieldInput"
    android:layout_marginRight="0dip"/>

</LinearLayout>

Its read as PagedFragment-RecordDataFieldBox, its a RecordDataFieldBox thats within a Fragment thats within a ViewPager.

Upvotes: 0

Views: 324

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24460

I took your first sample and tried it out here. I get no error wrapping it or not.

TextViewInherit.cs:

using Android.Content;
using Android.Util;
using Android.Widget;

namespace InflationShiz
{
    public class TextViewInherit : TextView
    {
        public TextViewInherit(Context context, IAttributeSet attrs) :
            this(context, attrs, 0)
        {
        }

        public TextViewInherit(Context context, IAttributeSet attrs, int defStyle) :
            base(context, attrs, defStyle)
        {
        }
    }
}

One.axml:

<?xml version="1.0" encoding="utf-8"?>
<inflationshiz.TextViewInherit
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Two.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <inflationshiz.TextViewInherit
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

Both work when I inflate in my Activity like so:

var one = LayoutInflater.Inflate(Resource.Layout.One, null);
var two = LayoutInflater.Inflate(Resource.Layout.Two, null);

I find it hard to reproduce your issue; Your code is scattered over 3 different SO questions and even more scattered because you have created answers to your own question where you try to elaborate on your initial questions.

Upvotes: 1

Related Questions