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

来源:互联网 发布:慈云蚕丝被 知乎 编辑:程序博客网 时间:2024/04/25 11:39

【原文: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:windowTitleBackgroundStyle">@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
原创粉丝点击