简单的自定义标题栏

来源:互联网 发布:语玩刷金币软件下载 编辑:程序博客网 时间:2024/06/07 20:56

之前写项目时框架是别人搭的,标题栏是一个布局文件做了简单的封装

每次都要在新建的activity的xml布局文件中include

感觉有点麻烦

于是在原来项目的基础上又做了简单的处理

写了一个baseActivity

baseActivity的xml文件如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <include layout="@layout/title_template"/>    <FrameLayout        android:id="@+id/view_mainBody"        android:layout_width="match_parent"        android:layout_height="match_parent">    </FrameLayout></LinearLayout>
包括一个标题栏 和一个内容区
标题栏 简单地写了3个textview和2个imageButton如下
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/titleBar"    android:layout_width="match_parent"    android:layout_height="48dp"    android:background="@color/colorPrimaryDark">    <TextView        android:id="@+id/title_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:textColor="@color/white"        android:textSize="18sp"        android:text="我是标题"/>    <TextView        android:id="@+id/title_right_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_centerVertical="true"        android:layout_marginRight="16dp"        android:textColor="@color/white"        android:textSize="18sp"        android:text="右侧"/>    <TextView        android:id="@+id/title_left_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_centerVertical="true"        android:layout_marginLeft="16dp"        android:textColor="@color/white"        android:textSize="18sp"        android:text="左侧"/>    <ImageButton        android:id="@+id/title_image_right"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_alignParentRight="true"        android:layout_marginRight="16dp"        android:visibility="gone" />    <ImageButton        android:id="@+id/title_image_left"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_alignParentLeft="true"        android:layout_marginLeft="16dp"        android:visibility="gone" /></RelativeLayout>
BaseActivity代码如下
public class BaseActivity extends FragmentActivity {    // 主体显示    protected ViewGroup mainBody;    // 是否使用模板 true 使用模板,false 不使用模板    protected boolean isTemplate = true;    protected Title titleBar;    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.template);        mainBody = (ViewGroup) findViewById(R.id.view_mainBody);        titleBar = new Title((RelativeLayout) findViewById(R.id.titleBar));        if (!isTemplate) {            titleBar.hide();        }    }    @Override    public void setContentView(int layoutResID) {        if (layoutResID == R.layout.template) {            super.setContentView(layoutResID);        } else {            mainBody.removeAllViews();            mainBody.addView(this.getLayoutInflater().inflate(layoutResID, null));        }    }    @Override    public void setContentView(View view) {        mainBody.removeAllViews();        mainBody.addView(view);    }    @Override    public void setContentView(View view, ViewGroup.LayoutParams params) {        mainBody.removeAllViews();        mainBody.addView(view, params);    }}
这样在我们需要使用带有标题栏的 activity时 只要继承我们的这个baseActivity就可以了
@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    titleBar.setTitleType(Title.TYPE6)            .setTitle("商城")            .setBackgroundResource(R.color.black)            .AddEventListener(Title.TITLE_LEFT_TEXT, new ICallBack() {                @Override                public void CallBackFunction(Object e) {                     finish();                }            });}
其中主要的对于标题栏的处理 在title这个类中,详情可以看下源码,封装的不是很好,但也可以起到一个参考作用吧。
本人QQ:3553917035 有不明白的可以 一起聊聊

0 0