标签:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_top" android:layout_width="fill_parent" android:layout_height="@dimen/top_height" android:background="#f8f8f8" > <Button android:id="@+id/btn_top_back" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerVertical="true" android:gravity="center" android:background="@drawable/btn_top_back" android:textSize="20sp" /> <TextView android:id="@+id/tv_top_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="@string/app_name" android:textColor="@color/black" android:textSize="20sp" /> <Button android:id="@+id/btn_top_right" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="@dimen/image_corner_radius" android:background="@null" android:gravity="center" android:textColor="@drawable/btn_top_right_selector" android:textSize="16sp" /> <TextView android:layout_width="fill_parent" android:layout_alignParentBottom="true" android:layout_height="1dp" android:background="@color/gray" /> </RelativeLayout>
public class TitleView extends FrameLayout { private Button btn_top_back; private TextView tv_top_title; private Button btn_top_right; public TitleView(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.common_top, this); btn_top_back = (Button) findViewById(R.id.btn_top_back); tv_top_title = (TextView) findViewById(R.id.tv_top_title); btn_top_right = (Button) findViewById(R.id.btn_top_right); } public void setvisible(int left,int middle,int right){ btn_top_back.setVisibility(left); tv_top_title.setVisibility(middle); btn_top_right.setVisibility(right); } public void setTitle(String title){ tv_top_title.setText(title); } public void setLeftOnclickListener(OnClickListener onClickListener){ btn_top_back.setOnClickListener(onClickListener); } public void setRightOnclickListener(OnClickListener onClickListener){ btn_top_right.setOnClickListener(onClickListener); } public void setRightText(String text){ btn_top_right.setText(text); } }
在布局里面就可以通过包名+类名来重用title布局文件,在activity或者fragement里面初始化,通过TitleView的方法控制内容的显示和隐藏,以及内容的变化,控件事件的响应。
标签:
原文地址:http://www.cnblogs.com/androidsuperman/p/4580523.html