通过从xml扩充来扩展RelativeLayout?

时间:2013-07-26 14:38:01

标签: android inheritance relativelayout

我想创建一个名为TabView的类,它扩展了RelativeLayout,并从包含RelativeLayout的xml中膨胀。问题是它似乎没有像我预期的那样工作。我在构造函数中做错了吗?我知道layoutInflater.inflate返回View对象但是我必须使它等于什么?任何建议都值得赞赏!

private class TabView extends RelativeLayout {
    private int mIndex;

    public TabView(Context context) {
        super(context, null, R.attr.vpiTabPageIndicatorStyle);
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.tabview_title_subtitle, null);
    }

    public int getIndex() {
        return mIndex;
    }

    public void setTitle() {
        TextView title = (TextView)findViewById(R.id.title);
        title.setText("Followers");
    }

    public void setSubitle() {
        TextView subtitle = (TextView)findViewById(R.id.subtitle);
        subtitle.setText("435");
    }
}

以下是tabview_title_subtitle.xml

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

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Title"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/title"
        android:text="Subtitle"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:8)

首先,您的布局应如下所示:

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

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Title"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/subtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/title"
    android:text="Subtitle"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</merge>

另一方面,您最终会得到包含另一个RelativeLayout

RelativeLayout

二。像这样膨胀它:

layoutInflater.inflate(R.layout.tabview_title_subtitle, this);    

答案 1 :(得分:4)

您可以在android中看到以下blog用于创建和使用自定义视图。 并且可能使用

inflater.inflate(R.layout.view_color_options, this, true);

而不是

 layoutInflater.inflate(R.layout.tabview_title_subtitle, null);

是你需要做的。

答案 2 :(得分:2)

使用:

layoutInflater.inflate(R.layout.tabview_title_subtitle, this);

实际将膨胀的视图添加到TabView(现在您只需对其进行充气并立即丢弃它)。此外,如果要在xml布局中使用自定义视图,还要实现采用ContextAttributeSet的构造函数。