我有一个可重用的布局,叫做title

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

在我的活动中
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

最佳答案

您可以创建扩展framelayout/或任何其他所需内容的自定义类。
在它的构造函数中创建两个方法:inflate()和bindViews()

public class CustomView extends FrameLayout {

private Context mContext;
private LayoutInflater layoutInflater;
// All views here

public CustomView (Context context) {
    super(context);
    this.mContext = context;
    inflate();
    bindViews();

}

private void inflate() {
    layoutInflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    layoutInflater.inflate(R.layout.your_xml_layout, this, true);
}

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

添加您的方法,您就可以在任何需要的地方使用这个类。

关于android - 可重用布局android的类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16299225/

10-09 06:18