實現自定義頂部標題欄(自定義控件)

1.創建 layout_title.xml(自定義標題視圖)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_titlebar"
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:background="#ed4255">

    <TextView
        android:id="@+id/text_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:ellipsize="marquee"
        android:gravity="center_horizontal|center"
        android:singleLine="true"
        android:text="標題欄"
        android:textColor="#ffffffff"
        android:textSize="20dp" />

    <Button
        android:id="@+id/button_backward"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:drawablePadding="6dp"
        android:ellipsize="end"
        android:gravity="center"
        android:onClick="onClick"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:text="返回"
        android:background="#ED4255"
        android:textColor="#ffffffff"
        android:textSize="18dp"

        android:visibility="invisible" />
    <!--android:background="@drawable/title_button_selector"
    android:drawableLeft="@drawable/back_arrow"-->
    <Button
        android:id="@+id/button_forward"
        android:layout_width="70dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:drawablePadding="6dp"
        android:ellipsize="end"
        android:gravity="center"
        android:onClick="onClick"
        android:paddingLeft="5dp"
        android:singleLine="true"
        android:text="提交"
        android:textColor="#ffffffff"
        android:textSize="18dp"
        android:background="#ED4255"
        android:visibility="invisible" />
    <!-- android:background="@drawable/title_button_selector"-->
</RelativeLayout>

2.創建 TitleActivity

package com.example.titleactivitydemo2;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

public class TitleActivity extends Activity implements View.OnClickListener {

    //private RelativeLayout mLayoutTitleBar;
    private TextView mTitleTextView;
    private Button mBackwardbButton;
    private Button mForwardButton;
    private FrameLayout mContentLayout;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setupViews();//加載 activity_title 佈局 ,並獲取標題及兩側按鈕
    }

    private void setupViews() {
        super.setContentView(R.layout.activity_title);
        mTitleTextView = (TextView) findViewById(R.id.text_title);
        mContentLayout = (FrameLayout) findViewById(R.id.layout_content);
        mBackwardbButton = (Button) findViewById(R.id.button_backward);
        mForwardButton = (Button) findViewById(R.id.button_forward);
    }

    /**
     * 是否顯示返回按鈕
     *
     * @param backwardResid 文字
     * @param show   true則顯示
     */
    protected void showBackwardView(int backwardResid, boolean show) {
        if (mBackwardbButton != null) {
            if (show) {
                mBackwardbButton.setText(backwardResid);
                mBackwardbButton.setVisibility(View.VISIBLE);
            } else {
                mBackwardbButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * 提供是否顯示提交按鈕
     *
     * @param forwardResId 文字
     * @param show         true則顯示
     */
    protected void showForwardView(int forwardResId, boolean show) {
        if (mForwardButton != null) {
            if (show) {
                mForwardButton.setVisibility(View.VISIBLE);
                mForwardButton.setText(forwardResId);
            } else {
                mForwardButton.setVisibility(View.INVISIBLE);
            }
        } // else ignored
    }

    /**
     * 返回按鈕點擊後觸發
     *
     * @param backwardView
     */
    protected void onBackward(View backwardView) {
        Toast.makeText(this, "點擊返回,可在此處調用finish()", Toast.LENGTH_SHORT).show();
        //finish();
    }

    /**
     * 提交按鈕點擊後觸發
     *
     * @param forwardView
     */
    protected void onForward(View forwardView) {
        Toast.makeText(this, "點擊提交", Toast.LENGTH_SHORT).show();
    }


    //設置標題內容
    @Override
    public void setTitle(int titleId) {
        mTitleTextView.setText(titleId);
    }

    //設置標題內容
    @Override
    public void setTitle(CharSequence title) {
        mTitleTextView.setText(title);
    }

    //設置標題文字顏色
   @Override
    public void setTitleColor(int textColor) {
        mTitleTextView.setTextColor(textColor);
    }


    //取出FrameLayout並調用父類removeAllViews()方法
    @Override
    public void setContentView(int layoutResID) {
        mContentLayout.removeAllViews();
        View.inflate(this, layoutResID, mContentLayout);

        onContentChanged();
    }





    @Override
    public void setContentView(View view) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view);
        onContentChanged();
    }

    /* (non-Javadoc)
     * @see android.app.Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }


    /* (non-Javadoc)
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     * 按鈕點擊調用的方法
     */
    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.button_backward:
                onBackward(v);
                break;

            case R.id.button_forward:
                onForward(v);
                break;

            default:
                break;
        }
    }
}

3.MainActivity繼承TitleActivity

package com.example.titleactivitydemo2;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends TitleActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setTitle("標題欄");
        setTitleColor(Color.BLUE);
        showBackwardView(R.string.text_back,true);
        showForwardView(R.string.text_forward,true);

    }
}

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章