2

タイトルと呼ばれる再利用可能なレイアウトがあります

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:id="@+id/title_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:text="@string/lesson_title"
    android:textSize="@dimen/title" />

<Button
    android:id="@+id/title_book"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="onClick"
    android:text="@string/book" />

<Button
    android:id="@+id/title_buy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="onClick"
    android:text="@string/buy"
    android:visibility="gone" />

私はこれを私の活動のほとんどに含めていますが、うまくいきます。

クリックとタイトル テキストを処理するには、すべてのアクティビティでこれを行う必要があります。

TextView title = (TextView) findViewById(R.id.title_text);
title.setText(R.string.some_title);
Button book = (Button) findViewById(R.id.title_book);
Button buy = (Button) findViewById(R.id.title_buy);

そしてクリックのために

public void onClick(View v) {

    switch (v.getId()) {

    case R.id.title_book:
            Toast.makeText(getBaseContext(), "book", Toast.LENGTH_SHORT).show();
        break;

    case R.id.title_buy:
            Toast.makeText(getBaseContext(), "buy", Toast.LENGTH_SHORT).show();
        break;
    }
}

これを処理するクラスを作成する方法はありますか? ボタンは毎回同じ機能を実行するため、タイトルのテキストを設定するためにアクティビティから呼び出すことができる init メソッドを持つことができます。

これは、Udiの回答に基づいて私がやろうとしたことです

public class CustomTitle extends RelativeLayout{

private Context mContext;
private LayoutInflater layoutInflater;
private ViewHolder myViewHolder;
String mText = "hello";

public CustomTitle(Context context) {
    super(context);
    this.mContext = context;
    //this.mText = text;

    inflate();
    bindViews();
}

private void inflate() {

    if(layoutInflater == null){
        layoutInflater = (LayoutInflater) mContext
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    layoutInflater.inflate(com.callan.app.R.layout.title, this, true);      
}

private void bindViews() {
    // bind all views here

    if(myViewHolder == null){
        myViewHolder = new ViewHolder();
    }

    myViewHolder.title_text = (TextView) findViewById(com.callan.app.R.id.title_text);
    myViewHolder.book = (Button) findViewById(com.callan.app.R.id.title_book);
    myViewHolder.buy = (Button) findViewById(com.callan.app.R.id.title_buy);

    if(Callan.getMyCredit() > 0){

        myViewHolder.buy.setVisibility(View.GONE);
        myViewHolder.book.setVisibility(View.VISIBLE);
    }else{

        myViewHolder.buy.setVisibility(View.VISIBLE);
        myViewHolder.book.setVisibility(View.GONE);
    }

    myViewHolder.title_text.setText(mText);     
}

class ViewHolder{

    TextView title_text;
    Button book;
    Button buy;
}

public void onClick(View v) {

    switch (v.getId()) {

    case com.callan.app.R.id.title_book:
            Toast.makeText(mContext, "book", Toast.LENGTH_SHORT).show();
        break;

    case com.callan.app.R.id.title_buy:
            Toast.makeText(mContext, "buy", Toast.LENGTH_SHORT).show();
        break;
    }
}

}

私のxmlには

<com.callan.custom_views.CustomTitle xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" >

//my views

それから私の活動では、onCreateの後に持っています

CustomTitle cTitle = new CustomTitle(this);

例外

04-30 14:24:44.440: E/AndroidRuntime(7184): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.callm.app/com.callan.app.Lessons}:       android.view.InflateException: Binary XML file line #2: Error inflating class        com.callm.custom_views.CustomTitle
 04-30 14:24:44.440: E/AndroidRuntime(7184): Caused by: android.view.InflateException:       Binary XML file line #2: Error inflating class com.callm.custom_views.CustomTitle
4

2 に答える 2