0

After i looking up inflate, i found it quit hard to understand what does the root view mean. Is it just the parent view of this generated xml hierarchy or the uppest view of the whole view hierarchy.

I think the answer most probably be the former. But I am facing a problem. If in the case that i can't get the parent view of this generated hierarchy, do i have to set the layout parameter everytime i inflate a hierarchy tree?

Here is my code. And the LinearLayout can't fill the parent. I think the layout_height and layout_width of the LinearLayout must be set to wrap_content.

storage_status.xml

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

    <View
        android:layout_height="50px"
        android:layout_width="50px"
        android:background="#f00"/>

</com.android.contacts.StorageStatusView>

Snippets in java file.

private class QuickActionClickListener implements QuickAction.OnActionItemClickListener {

    @Override
    public void onItemClick(QuickAction source, int pos, int actionId) {
        switch (actionId) {
            case ACTION_ID_STORAGE_STATUS :
                final LayoutInflater inflater = getLayoutInflater();
                new AlertDialog.Builder(ContactsListActivity.this)
                        .setTitle(UIUtils.getString(ContactsListActivity.this, R.string.storage_status))
                        .setView(inflater.inflate(R.layout.storage_status, null))
                        .show();

                break; 
        }
    }
}

Thanks in advance. And sorry for my poor english.

Frank Cheng
  • 5,928
  • 9
  • 52
  • 80
  • I found somebody have asked this question before. And the answer is quit clear. http://stackoverflow.com/questions/5026926/making-sense-of-layoutinflater – Frank Cheng Feb 27 '12 at 07:14

1 Answers1

-1

As you said, it's just a parent of generated xml hierarchy, and you will have to provide layout parameters if you inflate the view with no parent specified.

a.ch.
  • 8,285
  • 5
  • 40
  • 53
  • Thanks @a.ch. I have tried to set layout parameters, but the view is still not fill parent. (When i did the same thing in the past, it always works.). I found there is another overload inflate method with a parameter called `attachToRoot`. I doubt if i set this flag to false then i can give a view not just the generated view's parent but the parent of parent. – Frank Cheng Feb 27 '12 at 07:04
  • How did you set these pararms? – a.ch. Feb 27 '12 at 07:08
  • View view = inflater.inflate(R.layout.storage_status, null); view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); – Frank Cheng Feb 27 '12 at 07:13