1.默认标题
android的默认标题是不显示出来的,需要我们去 AndroidManifest.xml 继续修改
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
tools:targetApi="31">
<activity
android:name=".SettingsActivity"
android:exported="false"
android:label="@string/title_activity_settings" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
里面的 android:theme就是他的主题样式也叫标题样式,现在的是默认隐藏他一个,还有一种方式就是换一个样式进行然他的显示和隐藏
接下来把android:theme 他里面的样式替换成我们自定义的样式,上代码
android:theme="@style/MyApplication_style"
这个名字可以随便起,不要太离谱就可以 然后 MyApplication_style 这个就是我们自定义的样式,放在 res/values/themes.xml 里面
<style name="CustomWindowTitleBackground">
<item name="android:background">@color/cardview_dark_background</item>
</style>
<style name="MyApplication_style" parent="Base.Theme.MyApplicatio">
<item name="android:windowNoTitle">true</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
这个里面的 <item name="android:windowNoTitle">true</item> 就是控制他的显示和隐藏,true就是隐藏,false就是显示
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
这个就是他的背景颜色 显示出来的效果 ,右边这个就是为 true 左边这个就是fasle
里面的这个哒哒哒哒哒的文字也可以进行替换 就是 AndroidManifest.xml里面的 android:label属性。这里不需要java代码就可以 把他显示和隐藏,我的是这样的 里面的activity_mainw文件,创建项目就存在
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
我发的图片里信息,会在我自定义标题里面使用
2.自定义标题
先上效果 右边这个是正确的效果,左边那个是没有隐藏掉默认标题,是防止有人忘记隐藏了,不过默认就是隐藏的,只要不动就不会像左边一样
这样的效果可能有点丑,就记录一下,要实现这样的效果可以先把默认的标题先关了,就是我们上面的默认标题 ,你可以设置成false或者直接使用
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
接下来是先是 settings_activity.xml主要要显示的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_scrollFlags="scroll|enterAlways" />
<FrameLayout
android:id="@+id/fragment_container_view"
android:background="@drawable/shape_gradient"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
里面的 <androidx.appcompat.widget.Toolbar>标签就是安卓提供的一个组件标签 这FrameLayout 是Android容器组件—FrameLayout,不太熟悉可以去看一下 其他大佬 ,如果需要里面的背景颜色的话自己取 shape_gradient.xml 这个文件放在 res/drawable/shape_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:endColor="#74ebd5"
android:startColor="#acb6e5" />
</shape>
接下来就是java代码。有两种的Activity,给你选择。
对于较新的Android版本(API级别14及以上),推荐使用AppCompatActivity
替代FragmentActivity
。AppCompatActivity
是FragmentActivity
的一个子类,提供了更好的向后兼容性和对Material Design特性的支持。此外,AppCompatActivity
还提供了对ActionBar和其他旧版UI组件的支持
package com.go.myapplicatio;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.preference.PreferenceFragmentCompat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//请求系统不要显示标题栏。
setContentView(R.layout.settings_activity);
Toolbar toolbar = findViewById(R.id.toolbar);//获取自定义的标题的资源
setSupportActionBar(toolbar);//把获取到的资源 放进去
if (savedInstanceState == null) {
getSupportFragmentManager()//打开另外一个片段
.beginTransaction()//打开事务
.replace(R.id.fragment_container_view, new SettingsFragment())//先删除在添加 add()是添加
.commit();//添加
}
ActionBar actionBar = getSupportActionBar();//获取资源有没有需要的Toolbar
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);//左上角 出现一个小箭头返回 是否可以点击
actionBar.setDisplayHomeAsUpEnabled(true);//左上角 出现一个小箭头返回
}
}
/**
* //加载菜单文件
* @param menu The options menu in which you place your items.
*
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.title_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/**
* 点击右上角的返回图片 就触发下面的方法
* @param item The menu item that was selected.
*
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
*允许你轻松地创建和管理复杂的设置界面,同时保持了对各种 Android 版本的兼容性。
*/
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
这个是第2种Activity,如果您想在继承自 FragmentActivity
的类中使用 ActionBar
,您需要确保您的 FragmentActivity
实际上是一个 AppCompatActivity
的实例,或者您可以通过调用 getActionBar()
方法(而不是 getSupportActionBar()
)来获取 ActionBar
。但是,请注意,getActionBar()
方法是在 API 级别 11(Android 3.0)中引入的,并且仅适用于原生 ActionBar
,而不提供 AppCompat
库提供的向后兼容功能
如果还没有看明白文字就上代码,就清楚了,这个是继承FragmentActivity这个的,效果都是一样的,就是换一个Activity,他们的区别
- 来源和兼容性:
FragmentActivity
来自 Support Library v4,主要用于支持 Fragment 的使用,特别是在旧版本的 Android 设备上。而AppCompatActivity
来自 Support Library v7,除了支持 Fragment 外,还提供了对 ActionBar 和 Material Design 风格的向后兼容支持。 - ActionBar 支持:虽然
FragmentActivity
本身不直接提供对 ActionBar 的支持,但AppCompatActivity
作为其子类,提供了对 ActionBar 的全面支持,并推荐使用ToolBar
。 - Material Design 支持:
AppCompatActivity
还为支持 Material Design 风格的控件提供了便利,而FragmentActivity
则没有这一功能。
package com.go.myapplicatio;
import android.widget.Toolbar;
import androidx.fragment.app.FragmentActivity;
import androidx.preference.PreferenceFragmentCompat;
import android.app.ActionBar;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.settings_activity);
Toolbar toolbar = findViewById(R.id.toolbar);//获取自定义的标题的资源
setActionBar(toolbar);//把获取到的资源 放进去
if (savedInstanceState == null) {
getSupportFragmentManager()//打开另外一个片段
.beginTransaction()//打开事务
.replace(R.id.fragment_container_view, new SettingsFragment())//先删除在添加 add()是添加
.commit();//添加
}
ActionBar actionBar = getActionBar();//获取资源有没有需要的Toolbar
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);//左上角 出现一个小箭头返回 是否可以点击
actionBar.setDisplayHomeAsUpEnabled(true);//左上角 出现一个小箭头返回
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* //加载菜单文件
*
* @param menu The options menu in which you place your items.
* @return
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.title_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/**
* 点击右上角的返回图片 就触发下面的方法
*
* @param item The menu item that was selected.
* @return
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/**
* 允许你轻松地创建和管理复杂的设置界面,同时保持了对各种 Android 版本的兼容性。
*/
public static class SettingsFragment extends PreferenceFragmentCompat {
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// addPreferencesFromResource(R.xml.root_preferences);
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
这个时候运行会报错,显示 java.lang.ClassCastException: androidx.appcompat.widget.Toolbar cannot be cast to android.widget.Toolbar ,这个是我们的 Toolbar的类型不同无法转换
把 settings_activity.xml里面的Toolbar换一个类型就可以了,我们使用安卓自己的
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_scrollFlags="scroll|enterAlways" />
<FrameLayout
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
已上就是继承FragmentActivity需要更改的东西,两个
Activity,选一个,你喜欢的就可以,效果都是一样的,千万不能弄混了
要想继承 PreferenceFragmentCompat 需要去 在你的 build.gradle
文件中,添加对 androidx.preference
库的引用。例如:
dependencies {
implementation 'androidx.preference:preference:1.2.0'
}
不要把原来存在的全部删除,就留下这一个,是添加上去
我的是这样的
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.preference:preference:1.2.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
res/menu/title_menu.xml 自定义菜单
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<item android:id="@+id/add"
android:orderInCategory="1"
android:title="设置"
app:showAsAction="ifRoom"
/>
</menu>
res/xml/root_preferences.xml 这个就是一个片段
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/messages_header">
<EditTextPreference
app:key="signature"
app:title="@string/signature_title"
app:useSimpleSummaryProvider="true" />
<ListPreference
app:defaultValue="reply"
app:entries="@array/reply_entries"
app:entryValues="@array/reply_values"
app:key="reply"
app:title="@string/reply_title"
app:useSimpleSummaryProvider="true" />
</PreferenceCategory>
<PreferenceCategory app:title="@string/sync_header">
<SwitchPreferenceCompat
app:key="sync"
app:title="@string/sync_title" />
<SwitchPreferenceCompat
app:dependency="sync"
app:key="attachment"
app:summaryOff="@string/attachment_summary_off"
app:summaryOn="@string/attachment_summary_on"
app:title="@string/attachment_title" />
</PreferenceCategory>
</PreferenceScreen>
string.xml
<resources>
<string name="app_name">哒哒哒哒哒哒</string>
<string name="title_activity_settings">万达大大大</string>
<!-- Preference Titles -->
<string name="messages_header">信息</string>
<string name="sync_header">同步</string>
<!-- Messages Preferences -->
<string name="signature_title">签名</string>
<string name="reply_title">默认回复操作</string>
<!-- Sync Preferences -->
<string name="sync_title">定期同步电子邮件</string>
<string name="attachment_title">下载传入的附件</string>
<string name="attachment_summary_on">自动下载传入电子邮件的附件
</string>
<string name="attachment_summary_off">仅在手动请求时下载附件</string>
</resources>
源代码贴完了
第二篇在下一个篇文章 android 实现默认标题和自定义标题二、自定义DialogFragment-优快云博客,效果图