0.Demo结构
snipaste20170215_003003.png1.新建自定义Title的布局文件
title布局文件.png代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg"
>
<!--第一步画出自定义Title控件-->
<Button
android:text="Back"
android:layout_gravity="center"
android:background="@drawable/back_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_back"
android:textColor="#fff"
android:layout_margin="5dp"
/>
<TextView
android:text="Title"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:gravity="center"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:textSize="24sp"
android:textColor="#fff"
/>
<Button
android:background="@drawable/edit_bg"
android:text="Edit"
android:layout_gravity="center"
android:textColor="#fff"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title_edit"
/>
</LinearLayout>
2.新建TitleLayout类 继承于LinearLayout
1.新建TitleLayout类.png代码:
package com.example.zhao.customview;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
/*第二步创建TitleLayout类 */
public class TitleLayout extends LinearLayout {
public TitleLayout(Context context, AttributeSet attrs) {
super(context, attrs);
//布局填充器--从 传入的上下文参数--进行填充
LayoutInflater.from(context).inflate(R.layout.title,this);
Button titleBack=(Button)findViewById(R.id.title_back);
Button titleEdit=(Button)findViewById(R.id.title_edit);
titleBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
((Activity)getContext()).finish();
}
});
titleEdit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getContext(),"Edit",Toast.LENGTH_LONG).show();
}
});
}
}
3.应用定义好的Title控件
主布局中应用组件.pngactivity_main.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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zhao.customview.MainActivity">
<!--在布局文件中应用写好的自定义组件类-->
<com.example.zhao.customview.TitleLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.zhao.customview.TitleLayout>
</RelativeLayout>
MainActivity.java代码:
package com.example.zhao.customview;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//应用前要隐藏原有的Title
ActionBar actionBar=getSupportActionBar();
if(actionBar!=null){
actionBar.hide();
}
}
}
网友评论