Раздувание XML-макета в настраиваемую ViewGroup

Я пытаюсь раздуть XML-макет в пользовательский ViewGroup. После расширения макета ViewGroup отображает только корневое представление макета, фактически он должен отображать полный файл макета в ViewGroup.

Я рассмотрел другие похожие вопросы, связанные с этим, на stackoverflow и других веб-сайтах, но ни один из них не помог.

Вот код моего кастомного ViewGroup:

public class ViewEvent extends ViewGroup {
private final String LOG_TAG="Month View Event";
public ViewEvent(Context context) {
        super(context); 
    }

    public ViewEvent(Context context,AttributeSet attrs) {
        super(context,attrs);   
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {      

        int childCount= getChildCount();
        for(int i=0;i<childCount;i++)
        {   
            Log.i(LOG_TAG, "Child: " + i + ", Layout [l,r,t,b]: " + l + "," + r + "," + t + "," + b);
            View v=getChildAt(i);
            if(v instanceof LinearLayout)
            {
                Log.i(LOG_TAG, "Event child count: " + ((ViewGroup)v).getChildCount()); // displaying the child count 1
                v.layout(0, 0, r-l, b-t);
            }
            //v.layout(l, r, t, b);
        }
    }   

В Activity я использую эту настраиваемую группу представлений:

ViewEvent event = new ViewEvent(getApplicationContext());
LayoutInflater inflator;
inflator=(LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflator.inflate(R.layout.try1, event);
setContentView(event);

Ниже приведен файл макета:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffaa77">


    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff0000"
        android:text="asdasadfas" />

</LinearLayout>

После надувания этого макета я получаю только root LinearLayout с цветом фона #ffff0000. TextView не отображается.

Где я делаю не так или что-то упускаю?


person Vinod Maurya    schedule 19.08.2011    source источник


Ответы (2)


попробуйте getParent() события (ваш ViewEvent):

inflator.inflate(R.layout.try1, event.getParent(), false);
person Badre    schedule 13.03.2013

Попробуйте вот так. Вы не указали идентификатор для своего TextView.

   <TextView 
            android:id="@+id/testTextId"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffff0000"
            android:text="asdasadfas" />

Затем получите доступ к этому TextView, как показано ниже.

TextView testTextView = (TextView) ViewEvent.findViewById(R.id.testTextId);
person jainal    schedule 19.08.2011
comment
Он доступен, но не отображается! - person Vinod Maurya; 19.08.2011