Android之自定义标题

  我们知道我们创建的每一个Activity,系统默认为我们提供了一下黑色的标题,本篇我将带领大家接触一下如何实现自定义标题样式。相比系统为我们提供的样式,自定义标题可以满足我们唯心所欲的自定义设计,使我们的界面看上去更加的高端上档次,以便更好的吸引用户的使用。下面开始今天的内容介绍:

  1、既然是自定义标题样式,首先我们需要设计一个自定义标题布局,通过这个布局文件,我们可以随心所欲的设计我们的标题样式(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="match_parent"
    android:orientation="horizontal"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#aa0000"
        android:text="这是我的自定义标题" 
        />
    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="更多"
        />

</LinearLayout>

  2、写好布局文件了,下面我们开始设计标题的样式,项目res目录下styles.xml:

<resources>

    <style name="itcastTheme" parent="android:Theme">
         <item name="android:windowContentOverlay">@color/nonecolor</item>
         <item name="android:windowTitleSize">44dp</item><!-- 设置自定义标题的宽度 -->
         <item name="android:windowTitleBackgroundStyle">@style/itcastbg</item><!-- 自定义标题的样式 -->
    </style>
    
    <style name="itcastbg">
        <item name="android:background">@drawable/rectangle</item>
    </style>

</resources>

  3、红色字体部分,是我通过drawable文件下的rectangle.xml文件实现的一个标题背景:

<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <gradient
        android:angle="270"
        android:endColor="#1DC9CD"
        android:startColor="#A2E0FB" />
    <padding
        android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
</shape>

  4、到这里我么就可以开始修改我们的主Activity:

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);//设置我们自定义标题
        Button  mybutton = (Button)findViewById(R.id.button);
        mybutton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "尽请期待", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

  需要注意的是红色部分必须写在引用布局文件之前,不然达不到效果。

  5、最后我们需要在AndroidManifest.xml文件中,为我们的Activity设置一下样式:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.edu.hpu.activity_title"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name">
        <activity
            android:name=".MainActivity" 
            android:theme="@style/itcastTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

  好了,关于Android自定义标题的介绍就说完了,感兴趣的小童鞋可以实现一下。新手学习,高手交流。

原文地址:https://www.cnblogs.com/AndroidJotting/p/4944788.html