- MainActivity:
- 只需要在onCreate()的中添加:Custom_Title title=new Custom_Title(this);
- MainActivity所对应的xml文件:
- <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mac.customtitle.MainActivity">
<com.example.mac.customtitle.Custom_Title
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout> - <com.example.mac.customtitle.Custom_Title>代表的是自定义标题的代码文件
- <?xml version="1.0" encoding="utf-8"?>
- 自定义标题栏代码:
- package com.example.mac.customtitle;
import android.content.Context;
import android.graphics.Color;
import android.provider.CalendarContract;
import android.support.annotation.ColorInt;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by mac on 16/8/11.
*/
public class Custom_Title extends LinearLayout implements View.OnClickListener{
public Custom_Title(Context context, AttributeSet attrs) {
super(context, attrs);
//加载布局
View view=LayoutInflater.from(context).inflate(R.layout.custom_title,this);
//设置布局控件
TextView textView=(TextView)view. findViewById(R.id.tv_title);
Button button_back= (Button)view. findViewById(R.id.bt_back);
Button button_next= (Button)view. findViewById(R.id.bt_next);
//给控件设置属性:
view.setBackgroundColor(Color.GRAY);
button_back.setBackgroundColor(Color.BLUE);
button_next.setBackgroundColor(Color.CYAN);
button_back.setOnClickListener(this);
button_next.setOnClickListener(this);
textView.setText("自定义标题");
}
public Custom_Title(Context context) {
super(context);
}
public Custom_Title(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.bt_back:
Toast.makeText(getContext(),"Im's back",Toast.LENGTH_LONG).show();
break;
case R.id.bt_next:
Toast.makeText(getContext(),"Im's next",Toast.LENGTH_LONG).show();
break;
}
}
}
- package com.example.mac.customtitle;
- XML文件:就是给Title布局:
- <?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="horizontal">
<Button
android:id="@+id/bt_back"
android:text="back"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:id="@+id/tv_title"
android:layout_gravity="center"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:text="自定义标题"
android:layout_height="match_parent" />
<Button
android:id="@+id/bt_next"
android:text="next"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout> - OK,自定义的标题栏就算完成了
- <?xml version="1.0" encoding="utf-8"?>