安卓学习征文 -- 自己定义标题栏

安卓不喜欢默认的标题栏,那么如何让自己成为一个自己的标题栏定义。

其标题栏的定义之后,例如下列:



首先这里须要定义一个自己定义的标题栏布局 title.xml文件 (里边须要两个图片这个非常easy)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     
    android:orientation="horizontal"  
    android:layout_width="fill_parent"   
    android:layout_height="50dp"       <!-- 新的 标题栏的 高度-->
    android:background="#f2f8f8">
      <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="40dp"
        android:layout_height="40dp"      
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:src="@drawable/lbt" />
      
      <TextView 
          android:layout_centerInParent="true"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:text="自己定义标题栏"
          android:textSize="20sp"
          android:gravity="center_vertical"
          />
    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_centerInParent="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"
        android:src="@drawable/rbt" />
    
</RelativeLayout>  

然后再MainActivity中声明使用自己定义的标题栏

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.title); 
	}

}

接下来我们再来设置标题栏的高度 以及 处理两側没有全然覆盖原始标题栏的bug。这时 我们须要在定义一个style文件 MyStyle.xml

在自己定义标题栏中常常回遇到没有填充全然的效果例如以下图:


 MyStyle.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="titleBarStyle" parent="android:Theme">
     <item name="android:windowTitleSize">50dp</item><!-- 原始标题栏的高度 -->
     <item name="android:padding">0dp</item><!-- 使新的标题栏全然延伸到对齐到原始标题栏的两边 -->
</style>
</resources>


然后呢 在 AndroidManifest.xml 中给activity 加入 一个theme属性

<?xml version="1.0" encoding="utf-8"?

> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.mytitle" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.mytitle.MainActivity" android:label="@string/app_name" android:theme="@style/titleBarStyle"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>


在一样中 有两种加入到标题栏高度设置。一个是原来的标题栏的高度。一个是他们的标题栏的高度定义。通常设置两个相等。否则,或自定义标题栏仅占原标题栏的上部。或者干脆在他们的一些自定义标题栏的显示。


在这里,基本完成修改。
源代码下载:http://download.csdn.net/detail/liuhenghui5201/7644123

原文地址:https://www.cnblogs.com/mfrbuaa/p/4602344.html