今天上Android课老师让做一个app的界面,不过界面实在是简单,仅仅是拖几个控件来就完成任务。所以我想自己好好研究一下,界面设计到底是如何做的。下面就先从系统自带的TitleBar开始。
首先,在新建一个Activity时,我感觉TitleBar很丑,
然后就想设成全屏的比较好看。所以有了:
<!--将AndroidManifest.xml修改一下就行了-->
android:theme="@style/AppTheme"
<!--改为-->
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
能够将TitleBar直接去掉,不过去掉我感觉更加的难看,于是就有了这篇文章的由来。我要自定义TitleBar,我要让TitleBar随着我的想法来展现而不是直接去掉!!!!
在网上找了一堆的资源,来说明如何自定义Titlebar的,总结了一下有几种方法(下面只是一种):
//layout下加入新的界面配置文件作为TitleBar的样式可以添加各种控件,Textview啊,Button啊。
//在style.xml里对Titlebar的大小及背景颜色进行修改。
//在AndroidManifest.xml将主题加入
//最后再在mainActivity中进行响应,就大功告成啦
</pre><pre name="code" class="java"><pre name="code" class="html"><span style="font-size:18px;"><!--<span style="font-family: Arial, Helvetica, sans-serif;">step 1 : 新建</span>mytitlebar.xml--></span>
</pre><pre name="code" class="html"><?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:layout_gravity="center_vertical"
android:gravity="center_vertical|top">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mytitlebar"
android:text="@string/btn_title_back"
android:textSize="20dp"
android:textColor="#3366cc"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt_title_string"
android:text="@string/app_title"
android:textSize="20dp"
android:textColor="#3366cc"
android:gravity="top" />
</LinearLayout>
</pre><pre name="code" class="html">
<span style="font-size:18px;"><!--Step 2: 修改style.xml--></span>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme">
<!-- Customize your theme here. -->
</style>
<style name="style_color" parent="android:Theme">
<item name="android:background">#342312</item>
</style>
<style name="My_Title_Style" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">@style/style_color</item>
<item name="android:windowTitleSize">50dp</item>
</style>
</resources>
<span style="font-size:18px;"><!--Step 3:修改AndroidManifest.xml--></span>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.like.java_study_helper" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_title"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_title"
<span style="color:#ff0000;">android:theme="@style/My_Title_Style"></span>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
</pre><pre name="code" class="html">
<span style="font-size:18px;">//Step 4:修改MainActivity.java中的onCreate函数</span>
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.mytitlebar);
}
}
按着这四个步骤就能简单的进行TitleBar的自定义布局啦!!!!
不过我这个时有点丑啦,,
不过也算是能够勉强达到要求,,
这些内容在网上大致相同,但是这不是我的目的;
未完待续。。。。。
在后续的开发中,还是遇到了一个很重要的问题!!!!
这种方式自定义的TitleBar只能在一个Activity中使用,在其他Activity中会出现程序自动stop的情况,具体如何解决,我将整理成一片博客来深入讨论这个问题。