bar_title.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_titlebar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@android:color/white">
<ImageView
android:id="@+id/iv_backward"
android:layout_width="60dp"
android:layout_height="match_parent"
android:ellipsize="none"
android:layout_gravity="center"
android:singleLine="true"
android:src="@drawable/arrow_back" />
<TextView
android:id="@+id/tv_title"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="marquee"
android:gravity="center_horizontal|center"
android:singleLine="true"
android:text="标题栏"
android:textColor="@android:color/black"
android:textSize="20dp" />
<TextView
android:id="@+id/tv_forward"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:ellipsize="end"
android:gravity="center"
android:paddingRight="5dp"
android:singleLine="true"
android:text="完成"
android:textColor="@android:color/holo_red_light"
android:textSize="18dp"/>
</LinearLayout>
TitleLayout
public class TitleLayout extends LinearLayout {
private ImageView iv_backward;
private TextView tv_title, tv_forward;
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LinearLayout bar_title = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.bar_title, this);
iv_backward = (ImageView) bar_title.findViewById(R.id.iv_backward);
tv_title = (TextView) bar_title.findViewById(R.id.tv_title);
tv_forward = (TextView) bar_title.findViewById(R.id.tv_forward);
if(ActivityCollector.getCurrentActivity().getClass().equals(PersonInfo.class)){
tv_forward.setText("保存");
tv_title.setText("编辑资料");
}
}
//设置监听器
//如果点击back则结束活动
iv_backward.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
((Activity)getContext()).finish();
}
});
}
public TextView getTextView_forward(){
return tv_forward;
}
}
主活动onCreate里添加activity管理,不然titlelayout获得不了当前页面的活动
ActivityCollector.addActivity(this);添加进activity管理,Titlelaout才能获得该活动,添加标题时使用
|