标签:
首先给大家推荐2个不错的android UI设计图库网站:http://iconmonstr.com/,http://iconfont.cn/。用到的两个png图片是从上面找的:
首先是自定义theme,不能用默认的主题,会报错;you cannot combined....。修改res/values/styles.xml:
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowActionBar">false</item>
</style>
<style name="CustomWindowTitleBackground">
<item name="android:background">#8A2BE2</item>
</style>
<style name="test" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources><item name="android:windowActionBar">false</item>或者自定义theme
<style name="test" parent="android:Theme">
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
推荐自定义theme,因为可以设置标题栏大小,背景色等属性
然后在AndroidMainfest.xml里修改引用的主题:
<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="<span style="background-color: rgb(0, 204, 204);">@style/test</span>" >
<activity
android:name=".MainActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>然后在res/layout下新建custom_title.xml自定义标题栏布局:
<?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="48dp"
android:orientation="horizontal" >
<ImageButton
android:id="@+id/imageBtn1"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#0044dd"
android:src="@drawable/edit"
android:layout_weight="1"
android:scaleType="fitXY" />
<TextView
android:layout_height="fill_parent"
android:layout_width="0dp"
android:layout_weight="3"
android:gravity="center"
android:padding="10dp"
android:text="消息"
android:textSize="21sp"/>
<ImageButton
android:id="@+id/imageBtn2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#00ff00"
android:scaleType="fitXY"
android:layout_weight="1"
android:src="@drawable/send"
/>
</LinearLayout>
这里加layout_weight属性设置权重1:3:1的权重布局,layout_width设置为0dp,它会根据权重来获取宽度.
然后在MainActivity.java onCreate加入如下代码:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
但是我却发现它刚开始是显示的是没有加载custom_title.xml布局的标题栏,然后才显示加载custom_title.xml布局的标题栏。
我猜测可能是
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.activity_main); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);这三行代码,第一行是声明要是用自定义的标题栏,但并为加载,第二句直接显示主界面,第三句才加载自定义的布局文件,所以这种方法并不好。
所以我们需要在主布局文件activity_main.xml里就加入自定义的标题栏布局
<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:background="#ffffff">
<include
android:id="@+id/titlebar1"
layout="@layout/custom_title"/>
</RelativeLayout>还需要设置theme为无标题栏,在res/values/styles.xml test主题下修改:
<style name="test" parent="android:Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowTitleSize">50dp</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>那么在MainActivity.java就不用修改了, onCreate方法里删除之前加的两句代码:
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);这样就不会在刚开始启动的时候没有加载自定义标题栏。
标签:
原文地址:http://blog.csdn.net/tingyuanss/article/details/46117129