自定义标题栏

这里用2种方法创建自定义标题栏
1. 改写原有的titlebar,实现的自定义标题栏
2. 创建自定义的控件与布局,来替换系统自带的titlebar

方法一

在MainActivity .java的oncreate()中

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);   
 setContentView(R.layout.main);   //软件activity的布局
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);  //titlebar为自己标题栏的布局,这里不再附属

以上代码顺序不能改变

这样虽然可以在一定程度上定制标题栏, 不过, 这里无法改变标题栏的高度和背景(背景设置之后会在两端有两个非常难看的边框). 据说, 原因是android 固有的.

这里有修改方法:

原理是这样的. 直接像上述代码那样添加title仅仅是把一个子界面添加到原有的title上的, 并没有改变原来的属性, 比如 标题栏大小, 标题栏背景. 这些需要在theme 主题里面定义.

因此先定义一个style,
若修改背景请修改android:windowTitleBackgroundStyle
若修改标题栏高度,请修改android:windowTitleSize
e.g.

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">


<style name="CustomWindowTitleBackground">
        <item name="android:background">#565656</item>
</style>


<style name="test" parent="android:Theme">
      <item name="android:windowTitleSize">50dp</item>
      <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>

在程序的android manifest.xml中对应activity中修改属性 android:theme = “@style/test” 就可以了

android:theme = "@style/test"

控件响应的方法与其他页面其他控件的方法一样

方法二

1.撤销系统提供的标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
2.创建titlebar自定义布局titlebar.xml
e.g.

<?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="wrap_content"
    android:layout_gravity="bottom"
    android:orientation="horizontal" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="button1"/>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_weight="1"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0dp"
        android:layout_gravity="center"
        android:text="Button2" />

</LinearLayout>

3.1引入标题布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    tools:context="com.exam.title_test.MainActivity" >


    <include layout="@layout/title_bar"/>

</RelativeLayout>

3.2 如果布局文件中的控件有事件请求的话。我们还要为事件注册代码,如果说事件无论在那个活动中,都是相同的,而要每一活动都要重新注册一次,无疑会增加重复代码,这种情况下最好使用自定义控件的方式来解决
e.g.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    tools:context="com.exam.title_test.MainActivity" >

    <com.exam.title_test.TitleLayout
        android:layout_height="wrap_content"
       android:layout_width="fill_parent">   
    </com.exam.title_test.TitleLayout>

</RelativeLayout>

我们从代码中可以看到,自定义控件与普通控件的引用方法是一样的,但是在添加时,需要指明包名与类名。
然后我们为标题中的控件注册点击事件,修改TitleLayout中代码
e.g.

public class TitleLayout extends LinearLayout {
    public TitleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        LayoutInflater.from(context).inflate(R.layout.title_bar,this);
        Button button1=(Button) findViewById(R.id.button1);
        Button button2=(Button) findViewById(R.id.button2);
        button1.setOnClickListener(new OnClickListener() ;
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getContext(), "button1",Toast.LENGTH_SHORT).show();
            }
        });
        button2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Toast.makeText(getContext(), "button2", Toast.LENGTH_SHORT).show(); 
            }
        });
    }

部分参考:自定义Android标题栏TitleBar布局

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值