Android自定义TitleBar 自定义标题栏 并进行事件处理

【原文: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"
     
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="blog.sina.com.cn/ruipheng"
        android:textColor="@android:color/background_dark"
        android:textSize="20dp"
          />


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="注册" />

</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>
    <style name="custom_window_title_background">
        <item name="android:background">@drawable/skinpic_blue</item>
    </style>
    <style name="custom_title">
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleBackgroundSty le">@style/custom_window_title_background</item>
    </style>  
</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("我的新浪博客");

}
});
下面是效果,右边为点击注册后

Android自定义TitleBar <wbr>自定义标题栏 <wbr>并进行事件处理 Android自定义TitleBar <wbr>自定义标题栏 <wbr>并进行事件处理




转载请注明出处: http://blog.sina.com.cn/ ruipheng

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值