【原文:http://blog.sina.com.cn/s/blog_62d3ddc00100z5u6.html】
Android自定义TitleBar 自定义标题栏 并进行事件处理
安卓自带的标题栏感觉很是难看,那么我们可以自定义titlebar
首先创建自定义标题栏xml文件 放在layout目录下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
</LinearLayout>
在代码中引入
//自定义标题栏
this.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.test);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.title_bar);
我们发现这样的话,标题栏的高度是原生的,不能满足我们的需求,于是我们在value目录下创建styles.xml文件
代码如下
<?xml version="1.0" encoding="utf-8"?>
<resources>
</resources>
这样自定义titlebar的工作就完成了,那么怎样处理titleBar中的事件呢!其实很简单,和处理本页面其他的控件一样的.我曾经以为处理上面的空间与自定义Dialog中xml的文件的方法一样,实际上我是错了,根本不需要那样处理
Button btn = (Button) findViewById(R.id.button1);
final TextView text = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
text.setText("我的新浪博客");
//System.out.println("我的新浪博客");
}
});
下面是效果,右边为点击注册后
转载请注明出处:
http://blog.sina.com.cn/
ruipheng