今天拿到了百度的一个demo,能接触到了一些大公司的代码风格,很不错,这里我会用三篇文章一一分析它的源码。
这篇文章分析代码中的标题特点,我会列出每一步:
1.1 添加标题
在drawable中创建title_background.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="#FFFFFF"
android:startColor="#C4C4C4" />
</shape>
1.2 创建自己的style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="mywindowTitleBackground">
<item name="android:background">@drawable/title_background</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="themeTitle" parent="android:Theme">
<item name="android:windowTitleSize">46dip</item>
<item name="android:windowTitleBackgroundStyle">@+style/mywindowTitleBackground</item>
<item name="android:windowBackground">@color/gray</item>
<item name="android:textColor">@color/black</item>
<item name="android:textSize">18dip</item>
</style>
</resources>
1.3 应用自己的style
在manifest.xml文件中为自己的主activity加入style
<activity
android:name="com.phoneapidemo.MainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:theme="@style/themeTitle" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
完毕,以后可以一直用这种style了,自己不用每次都去设计,加快应用的设计速度。