日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android之自定义标题

發(fā)布時(shí)間:2025/7/25 Android 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android之自定义标题 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  我們知道我們創(chuàng)建的每一個(gè)Activity,系統(tǒng)默認(rèn)為我們提供了一下黑色的標(biāo)題,本篇我將帶領(lǐng)大家接觸一下如何實(shí)現(xiàn)自定義標(biāo)題樣式。相比系統(tǒng)為我們提供的樣式,自定義標(biāo)題可以滿足我們唯心所欲的自定義設(shè)計(jì),使我們的界面看上去更加的高端上檔次,以便更好的吸引用戶的使用。下面開始今天的內(nèi)容介紹:

  1、既然是自定義標(biāo)題樣式,首先我們需要設(shè)計(jì)一個(gè)自定義標(biāo)題布局,通過這個(gè)布局文件,我們可以隨心所欲的設(shè)計(jì)我們的標(biāo)題樣式(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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#aa0000"android:text="這是我的自定義標(biāo)題" /><Button android:id="@+id/button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="更多"/></LinearLayout>

  2、寫好布局文件了,下面我們開始設(shè)計(jì)標(biāo)題的樣式,項(xiàng)目res目錄下styles.xml:

<resources><style name="itcastTheme" parent="android:Theme"><item name="android:windowContentOverlay">@color/nonecolor</item><item name="android:windowTitleSize">44dp</item><!-- 設(shè)置自定義標(biāo)題的寬度 --><item name="android:windowTitleBackgroundStyle">@style/itcastbg</item><!-- 自定義標(biāo)題的樣式 --></style><style name="itcastbg"><item name="android:background">@drawable/rectangle</item></style></resources>

  3、紅色字體部分,是我通過drawable文件下的rectangle.xml文件實(shí)現(xiàn)的一個(gè)標(biāo)題背景:

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

  4、到這里我么就可以開始修改我們的主Activity:

public class MainActivity extends Activity {@Overrideprotected 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);//設(shè)置我們自定義標(biāo)題Button mybutton = (Button)findViewById(R.id.button);mybutton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {Toast.makeText(MainActivity.this, "盡請期待", Toast.LENGTH_SHORT).show();}});} }

  需要注意的是紅色部分必須寫在引用布局文件之前,不然達(dá)不到效果。

  5、最后我們需要在AndroidManifest.xml文件中,為我們的Activity設(shè)置一下樣式:

<?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-sdkandroid:minSdkVersion="8"android:targetSdkVersion="21" /><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name"><activityandroid: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>

  好了,關(guān)于Android自定義標(biāo)題的介紹就說完了,感興趣的小童鞋可以實(shí)現(xiàn)一下。新手學(xué)習(xí),高手交流。

轉(zhuǎn)載于:https://www.cnblogs.com/AndroidJotting/p/4944788.html

總結(jié)

以上是生活随笔為你收集整理的Android之自定义标题的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。