2

我想创建一个名为 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>
4

3 回答 3

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);    
于 2013-07-26T14:46:58.987 回答
4

您可以查看以下博客,了解如何在 android 中创建和使用自定义视图。并且可能使用

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

代替

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

是你需要做的。

于 2013-07-26T14:45:09.523 回答
2

利用:

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

实际添加膨胀的视图TabView(现在你只是膨胀它并立即丢弃它)。此外,如果您想在 xml 布局中使用您的自定义视图,那么还要实现采用 aContext和 an的构造函数AttributeSet

于 2013-07-26T14:43:53.970 回答