开发者

Android: Same xml acts differently depending on where I inflate it and I don't know why

Why do some views in a linearlayout within a scrollview fill the parent when fill_parent is set when inflated from xml directly but don't when added via a custom component that inflates the same xml?

The result of inflating the same xml in two different places:

Android: Same xml acts differently depending on where I inflate it and I don't know why

The top item of the two is as a result of inflating the xml in a class that extends linearLayout and then adding that class to the linearLayout inside the scroll view. This view isn't filling the parent and I can't work out why.

The bottom item is the result of inflating the xml directly in the activity that this is all contained within. It appears as I expected.

This is the code that adds the two views in the activity:

scrollList.addView(new CommunityTaskListItem(this, null));
scrollList.addView(View.inflate(getBaseContext(), R.layout.communitytask, null));

This is the code that inflates the activity within a custom component class "CommunityTaskListItem":

View communityTask = View.inflate(context, R.layout.communitytask, null);
addView(communityTask);

I assume the xml is okay because it works fine when inflated directly in the activity. My question is why the fill_parent property seems to be lost when the xml is inflated in a custom component's class?

For good measure, here is the xml being inflated: EDIT: I gave you the wrong one before!!! Here's the right one:

<?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="wrap_content">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
        <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
    </LinearLayout>

</LinearLayout>

I'd like to keep the custom component and not inflate the xml within my activity if I could.

EDIT: Code where CommunityTaskListItem is inflated:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CommunityTaskListItem extends LinearLayout {

    TextView textViewCommunityTaskTimes;
    TextView textViewCommunityTaskDescription;

    public CommunityTaskListItem(Context context, AttributeSet attrs) {
        super(context, attrs);

        View communityTask = View.inflate(context, R.layout.communitytask, null);
        addView(communityTask);

        textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
        textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);

    }
...

EDIT: All working now! Here's the final code for anyone else incase they have a similar issue: Contructor for the custom object:

    public CommunityTaskListItem(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOrientation(VERTICAL);
        addView(inflate(context, R.layout.communitytask, null));

        textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
        textViewCommunityTaskDescription =     (TextView)findViewById(R.id.textViewCommunityTaskDescription);
    }

Custom object xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
        <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
        <TextView android:layout_width="fill_parent" android:layout_he开发者_高级运维ight="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
    </LinearLayout>

</LinearLayout>

To be honest I don't know exactly what the mistake I was making was so if someone could make that clear I'd be very grateful.


The first line should be:

scrollList.addView(new CommunityTaskListItem(this, null)
    new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

You call inflate() inside CommunityTaskListItem class and inflated view becomes a child view of the CommunityTaskListItem view. But you don't set width and height for this view so it doesn't match its parent's width.

EDIT: I think your XML should look like this:

<?xml version="1.0" encoding="utf-8"?>
<packagename.CommunityTaskListItem
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
        <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
    </LinearLayout>

</packagename.CommunityTaskListItem>

where packagename is the name of the package of the CommunityTaskListItem class.

In this case you should change the CommunityTaskListItem:

public class CommunityTaskListItem extends LinearLayout {

    TextView textViewCommunityTaskTimes;
    TextView textViewCommunityTaskDescription;

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

    @Override
    protected void onFinishInflate() {
        textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
        textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);
    }

    // ....
}

This class cannot be created using its constructor. It can be only inflated.

The second way is to inflate children in the constructor like you do. But the XML should differ:

<?xml version="1.0" encoding="utf-8"?>
<merge
  xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:background="@drawable/plainbutton" android:layout_marginBottom="5px" android:layout_marginTop="5px" android:layout_weight="1">
        <TextView android:layout_height="wrap_content" style="@style/CommunityTaskText" android:layout_width="fill_parent" android:layout_weight="1" android:id="@+id/textViewCommunityTaskTimes" android:text="xx:xx - xx:xx"></TextView>
        <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
    </LinearLayout>

</merge>

The CommunityTaskListItem will be: public class CommunityTaskListItem extends LinearLayout {

    TextView textViewCommunityTaskTimes;
    TextView textViewCommunityTaskDescription;

    public CommunityTaskListItem(Context context) {
        this(context, null);
    }

    public CommunityTaskListItem(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOrientation(VERTICAL);
        inflate(context, R.layout.communitytask);

        textViewCommunityTaskTimes = (TextView)findViewById(R.id.textViewCommunityTaskTimes);
        textViewCommunityTaskDescription = (TextView)findViewById(R.id.textViewCommunityTaskDescription);
    }

    // ....
}

This class can be created using its contructor and inflated from an XML. But if you want to inflate it, you should create a separate XML file. Let's call it task.xml.

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

And that's how you can use it:

scrollList.addView(new CommunityTaskListItem(this),
    new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
scrollList.addView(View.inflate(getBaseContext(), R.layout.task, null));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜