android 直接修改标题栏控件

  • 内容
  • 评论
  • 相关

MainActivity

package com.example.titlebar;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.support.v4.view.ViewGroupCompat;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroupOverlay;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private TextView myTextView;
	
	private void getTextView(ViewGroup vg){
		int tCount = vg.getChildCount();
		for(int i = 0; i < tCount; i++){
			View myView = vg.getChildAt(i);
			if (myView instanceof ViewGroup) {
				getTextView((ViewGroup) myView);
			}else if(myView instanceof View) {
				Log.d("obob","gbgb");
				if (myView instanceof TextView && myTextView == null) {
					Log.d("obob","gbgb11");
					myTextView = (TextView) myView;
				}
			}
		}
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Window w = getWindow();
		ViewGroup v = (ViewGroup) w.getDecorView();
		
		getTextView(v);
		
		if (myTextView != null) {
			myTextView.setText("标题发生变化");
			myTextView.setTextColor(Color.RED);
			myTextView.setBackgroundColor(Color.BLUE);
			myTextView.setClickable(true);
			
			myTextView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, "单击标题文字", Toast.LENGTH_LONG).show();
					
				}
			});
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</LinearLayout>

完整项目源码文件
http://pan.baidu.com/s/1bnAFI4R

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注