android自定义标题栏。。。。。。。

来源:互联网 发布:淘宝店铺属性怎么改 编辑:程序博客网 时间:2024/06/03 23:00

这尼玛真是蛋疼啊,弄了我一天啊这小小的标题栏

第一步在android工程目录下新建一个title.xml文件用于新建标题栏主题

<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android">    <style name="CustomizedWindowTitleBackground">        <!--修改标题栏背景颜色 -->        <item name="android:background">#047BF0</item>    </style>    <!-- parent只能用android:Theme默认主题,使用其他会出错-->    <style name="titleBar" parent="android:Theme">        <item name="android:windowTitleSize">48dp</item>        <item name="android:windowTitleBackgroundStyle">@style/CustomizedWindowTitleBackground</item>        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style></resources>

这尼玛坑爹啊 parent="android:Theme"只能用这个默认的主题啊!!!!!! 尼玛改了其他主题报错啊!!!!!!!


you cannot combine custom titles with other会报这个错


第二步在layout文件下新建一个titleBar的布局文件,这是标题栏的布局文件

<span style="font-size:12px;background-color: rgb(255, 255, 255);"><?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">    <Button        android:id="@+id/backButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="返回"        />    <Spinner        android:id="@+id/weekSpinner"        android:layout_width="wrap_content"        android:layout_height="wrap_content">    </Spinner></LinearLayout></span>

第三部

package com.example.administrator.myapplication;import android.app.Activity;import android.os.Bundle;import android.view.Window;public class ScheduleActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_class__schedule);        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);    }}</span>

在你要使用标题栏的Activity中添加这两句 

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);


其中第一句必须在

        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_class__schedule);

这两句之前调用,
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);

在setContentView之后调用就行

最后在manifests文件中

在你想要调用这个标题栏的Activity中添加

android:theme="@style/titleBar"
这么一行就行了

0 0
原创粉丝点击