android定制activity标题栏

来源:互联网 发布:淘宝如何退货 编辑:程序博客网 时间:2024/04/30 14:57

第一步

自定义标题栏控件
标题栏控件布局文件title_layout.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:background="#135D61"    android:orientation="horizontal" >    <Button        android:id="@+id/title_back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_margin="5dp"        android:gravity="center"        android:text="Back"        android:textColor="#841515" />    <TextView        android:id="@+id/textView1"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_weight="1"        android:gravity="center"        android:text="this is title"        android:textColor="#841515"        android:textSize="25sp" />    <Button        android:id="@+id/title_edit"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:gravity="center"        android:text="Edit"        android:textColor="#841515"         android:layout_gravity="center"/></LinearLayout>

自定义标题栏控件java代码,继承LinearLayout

package com.vincent.org.networkapp;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.util.AttributeSet;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;public class TitleLayout extends LinearLayout {    protected static final String Tag = "TitleLayout";    private final EditText editText;    public TitleLayout(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        editText = new EditText(context);        //加载自定义控件布局文件        View view = LayoutInflater.from(context).inflate(R.layout.title_layout,                this);        Button back = (Button) view.findViewById(R.id.title_back);        Button editbButton = (Button) view.findViewById(R.id.title_edit);        final Dialog dialog = new Dialog();//这里是关于自定义标题栏控件的按钮被按下了怎么事件处理。这里很简单就是弹出一个AlertDialog对话框。        back.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                Log.i(Tag, "back click on title");                ((Activity) getContext()).finish();            }        });        editbButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                Log.i(Tag, "edit click on title");                new AlertDialog.Builder(getContext()).setTitle("title Edit")                        .setIcon(android.R.drawable.btn_dialog)                        .setView(editText).setPositiveButton("ok", dialog)                        .setNegativeButton("cancel", dialog).show();            }        });    }    class Dialog implements DialogInterface.OnClickListener {        @Override        public void onClick(DialogInterface dialog, int which) {            // TODO Auto-generated method stub            switch (which) {            case -1:// positive                Log.i(Tag, "dialog positive button cancel"                        + editText.getText().toString());                if (editText.getParent() != null) {                    ((ViewGroup) editText.getParent()).removeAllViews();                }                dialog.cancel();                break;            case -2:// negative                Log.i(Tag, "dialog negative button dismiss"                        + editText.getText().toString());                if (editText.getParent() != null) {                    ((ViewGroup) editText.getParent()).removeAllViews();                }                dialog.dismiss();                break;            case -3:// Neutral                break;            default:                break;            }        }    }}

使用这个自定义控件的activity
只要在对应的activity的java代码中删除标题。

requestWindowFeature(Window.FEATURE_NO_TITLE);// 这个应用没有title

然后在这个activity对应的布局文件加入自定义控件的组件
即可,因为是自定义标题控件,所以这个自定义控件要在所有的控件之前。

<LinearLayout 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"    android:background="#1EB5A0"    android:orientation="vertical"    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.vincent.org.networkapp.CustomeTitleActivity" > <!-- 自定义标题栏控件-->    <com.vincent.org.networkapp.TitleLayout        android:id="@+id/title_widget_custom"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world"        android:textSize="50sp" /></LinearLayout>
0 0
原创粉丝点击