分享:Android之自定义标题

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

 

1、既然是自定义标题样式,首先咱们须要设计一个自定义标题布局,经过这个布局文件,咱们能够为所欲为的设计咱们的标题样式(title.xml):android

 

<?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.xmlapp

<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文件实现的一个标题背景:ide

<?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设置一下样式:this

<?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自定义标题的介绍就说完了,固然,开发完APP也是须要进行全方位的检测:www.ineice.comspa

相关文章
相关标签/搜索