勇敢直面人生

伟大的成绩和辛勤的劳动是成正比例的,有一分劳动就有一份收获,日积月累,从少到多,奇迹就可以创造出来

android 自定义title

package com.xiangyu.su;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

public class BasicActivity extends Activity implements OnClickListener {
	private TextView mTitleTextView;
	private Button mBackwardButton;
	private Button mForwardButton;
	private FrameLayout mContentLayout;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setupViews();
	}
	private void setupViews(){
		super.setContentView(R.layout.activity_title);
		mTitleTextView=(TextView) findViewById(R.id.text_title);
		mContentLayout=(FrameLayout) findViewById(R.id.layout_content);
		mBackwardButton=(Button) findViewById(R.id.button_backward);
		mForwardButton=(Button) findViewById(R.id.button_forward);
		mBackwardButton.setOnClickListener(this);
		mForwardButton.setOnClickListener(this);
	}
	protected void showBackwardView(int backwardResid,boolean show){
		if(mBackwardButton!=null){
			if(show){
				mBackwardButton.setText(backwardResid);
				mBackwardButton.setVisibility(View.VISIBLE);
			}else{
				mBackwardButton.setVisibility(View.INVISIBLE);
			}
		}
	}
	protected void showForwardView(int forwardResid,boolean show){
		if(mForwardButton!=null){
			if(show){
				mForwardButton.setVisibility(View.VISIBLE);
				mForwardButton.setText(forwardResid);
			}else{
				mForwardButton.setVisibility(View.INVISIBLE);
			}
		}
	}
	private void onBackward(View backwardView){
		Toast.makeText(this, "返回", Toast.LENGTH_SHORT).show();
	}
	protected void onForward(View forwardView) {
        Toast.makeText(this, "提交", Toast.LENGTH_LONG).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);
    }

    @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, LayoutParams params) {
        mContentLayout.removeAllViews();
        mContentLayout.addView(view, params);
        onContentChanged();
    }
	@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;
		}
	}
    

}

  

<?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/layout_titlebar"/>
    
    <FrameLayout 
        android:id="@+id/layout_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFF">
        
    </FrameLayout>

</LinearLayout>

  

<?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:gravity="center_horizontal|center"
        android:singleLine="true"
        android:text="标题栏"
        android:textColor="#ffffffff"
        android:textSize="20dp"/>
	<Button 
	    android:id="@+id/button_backward"
	    android:layout_width="60dp"
	    android:layout_height="match_parent"
	    android:background="@drawable/title_button_selector"
	    android:drawableLeft="@mipmap/back_arrow"
	    android:drawablePadding="6dp"	    
	    android:gravity="center"	    
	    android:paddingLeft="5dp"
	    android:singleLine="true"
	    android:text="返回"
	    android:textColor="#ffffffff"
	    android:textSize="18dp"
	    android:visibility="invisible"/>
	<Button 
	    android:id="@+id/button_forward"
	    android:layout_width="60dp"
	    android:layout_height="match_parent"
	    android:layout_alignParentRight="true"
	    android:background="@drawable/title_button_selector"
	    android:drawablePadding="6dp"	    
	    android:gravity="center"	    
	    android:paddingLeft="5dp"
	    android:singleLine="true"
	    android:text="提交"
	    android:textColor="#ffffffff"
	    android:textSize="18dp"
	    android:visibility="invisible"/>
</RelativeLayout>

  使用方法:

继承这个activity,重写onclick()方法

posted on 2016-10-13 15:16  想像你那么优秀  阅读(625)  评论(0编辑  收藏  举报

导航