Android:相同的xml行为不同,取决于我膨胀它的位置,我不知道为什么

时间:2011-09-11 11:03:27

标签: android scrollview fill-parent

为什么在从xml直接膨胀时设置fill_parent时,滚动视图中的linearlayout中的某些视图会填充父视图,但是当通过自定义组件添加膨胀相同的xml时却不会填充它?

在两个不同的地方膨胀相同的xml的结果:

http://imgur.com/DF6kl

两者的最高项是在扩展linearLayout的类中膨胀xml,然后将该类添加到滚动视图内的linearLayout。这个观点没有填补父母,我无法解决原因。

底部项是直接在活动中对xml进行膨胀的结果。它看起来像我预期的那样。

这是在活动中添加两个视图的代码:

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

这是在自定义组件类“CommunityTaskListItem”中膨胀活动的代码:

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

我认为xml没问题,因为它直接在活动中充气时工作正常。我的问题是为什么当xml在自定义组件的类中膨胀时,fill_parent属性似乎丢失了?

为了更好的衡量,这里是xml被夸大:编辑:我之前给了你错误的!这是正确的:

<?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>

我想保留自定义组件,如果可以的话,不要在我的活动中膨胀xml。

编辑:CommunityTaskListItem膨胀的代码:

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);

    }
...
编辑:现在全部工作!这是其他任何人的最终代码,因为他们有类似的问题: 自定义对象的构造函数:

    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);
    }

自定义对象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_height="wrap_content" android:layout_weight="1" style="@style/CommunityTaskText" android:id="@+id/textViewCommunityTaskDescription" android:text="Task"></TextView>
    </LinearLayout>

</LinearLayout>

说实话,我不知道我所犯的错误究竟是什么,如果有人能说清楚我会非常感激。

1 个答案:

答案 0 :(得分:2)

第一行应该是:

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

您在inflate()课程内拨打CommunityTaskListItem,充气视图成为CommunityTaskListItem视图的子视图。但是您没有为此视图设置宽度和高度,因此它与父视图的宽度不匹配。

编辑:我认为您的XML应如下所示:

<?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>

其中packagenameCommunityTaskListItem类的包的名称。

在这种情况下,您应该更改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);
    }

    // ....
}

无法使用其构造函数创建此类。它只会被夸大。

第二种方法是像你一样在构造函数中给孩子们充气。但XML应该有所不同:

<?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>

CommunityTaskListItem将是:     公共类CommunityTaskListItem扩展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);
    }

    // ....
}

可以使用其构造函数创建此类,并从XML中进行扩展。但是如果你想要膨胀它,你应该创建一个单独的XML文件。我们称之为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"/>

这就是你如何使用它:

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