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