Activity标题栏设置

在应用过程中需要将Activity的标题栏进行修改,一般情况下,通过java代码的setTitle方法可以设置其标题内容。在实际应用过程中可能会有复杂的需求,此时需要进行相关的处理,比如在标题栏上显示两个按钮和一个文本,按钮进行操作可以进行操作。这个时候就需要进行处理,处理方式如下:

1、建立title所需的布局文件:

<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_horizontal"
>
<Button android:text="返回"
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_middle_normal"
android:layout_marginLeft="20dp"
/>
<TextView android:text="欢迎使用"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:typeface="monospace"
android:layout_marginLeft="15dp"
/>
<Button android:text="下一步"
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/btn_middle_normal"
android:textSize="15dp"
android:typeface="monospace"
android:layout_marginLeft="15dp"
/>
</LinearLayout>

2、在java代码部分进行设置

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
       requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 注意顺序   
       setContentView(R.layout.declaration); // 注意顺序   
       getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,      // 注意顺序   
       R.layout.title);   
  }

到此步骤可以修改标题栏,但是不能设置标题栏的大小和背景。还需要处理

3、编写style.xml

<?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>

 

4在清单文件中配置

        <activity android:name=".DeclarationActivity"
                  android:label="@string/app_name"
                  android:theme="@style/test"
                  >

这样可以完成配置了。如果想完成更复杂的标题栏,那么需要通过java代码进行自定义。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值