Android学习:自定义Android标题栏(转载+原创)

    很多网友发现自己Android程序的标题栏TitleBar区域很单调,如果想个性化一些可以通过下面的方法来为自己软件的标题
定制一个layout布局文件,比如浏览器的标题栏,它包含了网站的Favicon,自定义的进度条,和不确定的进度指示等等,
实现的方法自己控制吧。下面代码在onCreate中使用,同时顺序不要改变,否则将无法生效:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);   
setContentView(R.layout.login);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
Layout下建立titlebar.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="horizontal" >   
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="私人相册"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:textColor="#EEEEEE" />
</RelativeLayout>
这样虽然可以在一定程度上定制标题栏, 不过, 这里无法改变标题栏的高度和背景(背景设置之后会在两端有两个非常难看的边框). 据说, 原因是android 固有的
这里有修改方法:
  原理是这样的. 直接像上述代码那样添加title仅仅是把一个子界面添加到原有的title上的, 并没有改变原来的属性,
比如 标题栏大小, 标题栏背景. 这些需要在theme 主题里面定义
  因此先定义一个style(在/res/values目录下), 若修改背景请修改android:windowTitleBackgroundStyle
  若修改标题栏高度,请修改android:windowTitleSize
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomWindowTitleBackground">
       <item name="android:background">@drawable/title</item>
</style>
<style name="titleBar" parent="android:Theme">
     <item name="android:windowTitleSize">40dp</item>
     <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
背景的定义文件 为:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
	android:shape="rectangle" android:padding="3dp">
	<gradient 
           android:startColor="#8D0000"
           android:centerColor="#A00000"
           android:endColor="#8D0000"
           android:angle="90"
           />
	<corners android:bottomRightRadius="3dp"
		android:bottomLeftRadius="3dp" android:topLeftRadius="3dp"
		android:topRightRadius="3dp"
		 />
</shape>
在程序的android manifest.xml中对应:
activity中添加属性 android:theme = "@style/titleBar"
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".LoginActivity"
            android:label="@string/title_activity_main"
            android:theme ="@style/titleBar"   
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
实现效果:








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值