Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android 自定義title,androidtitle

android 自定義title,androidtitle

編輯:關於android開發

android 自定義title,androidtitle


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()方法

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved