2

RelativeLayout を拡張する TabView という名前のクラスを作成したいと考えています。これは、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

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(現在は膨張させてすぐに破棄します)。Contextまた、カスタム ビューを xml レイアウトで使用する場合は、とを受け取るコンストラクタも実装しますAttributeSet

于 2013-07-26T14:43:53.970 に答える