可重用布局android的类

时间:2013-04-30 11:51:25

标签: android android-layout android-view

我有一个名为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

然后在我的活动中,我接着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

2 个答案:

答案 0 :(得分:2)

您可以创建自定义类,扩展FrameLayout /或您想要的任何其他内容。 在它的构造函数中创建2个方法: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
}
}

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

答案 1 :(得分:1)

您可以通过扩展某些标准视图来创建自定义小部件

http://developer.android.com/guide/topics/ui/custom-components.html#basic