android 实现默认标题和自定义标题一

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替代FragmentActivityAppCompatActivityFragmentActivity的一个子类,提供了更好的向后兼容性和对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,他们的区别

  1. 来源和兼容性FragmentActivity 来自 Support Library v4,主要用于支持 Fragment 的使用,特别是在旧版本的 Android 设备上。而 AppCompatActivity 来自 Support Library v7,除了支持 Fragment 外,还提供了对 ActionBar 和 Material Design 风格的向后兼容支持。
  2. ActionBar 支持:虽然 FragmentActivity 本身不直接提供对 ActionBar 的支持,但 AppCompatActivity 作为其子类,提供了对 ActionBar 的全面支持,并推荐使用 ToolBar
  3. 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-CSDN博客,效果图  

  • 22
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要修改Android Studio标题栏,您需要在styles.xml文件编辑应用程序的主题。具体步骤如下: 1. 打开Android Studio并打开您的项目。 2. 在左侧的Project面板,展开app文件夹并找到res文件夹。 3. 在res文件夹,找到values文件夹并打开styles.xml文件。 4. 在styles.xml文件,找到AppTheme并添加以下代码: <item name="android:windowNoTitle">true</item> <item name="windowActionBar">false</item> 这将隐藏默认标题栏。 5. 如果您想要自定义标题栏,可以在styles.xml文件添加以下代码: <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item> <item name="android:windowTitleStyle">@style/CustomWindowTitleText</item> 6. 在styles.xml文件,添加以下代码以定义自定义标题栏的样式: <style name="CustomWindowTitleBackground"> <item name="android:background">#FF000</item> </style> <style name="CustomWindowTitleText"> <item name="android:textColor">#FFFFFF</item> </style> 7. 保存并关闭styles.xml文件。 现在,您的应用程序将使用自定义标题栏。请注意,如果您使用了AppCompat库,则需要在styles.xml文件使用AppCompat主题。 ### 回答2: Android Studio提供了很多灵活的功能,允许我们对应用程序的各个方面进行定制和修改。其之一就是标题栏的修改。标题栏是指位于应用程序顶部的部分,通常包括应用程序名称和其他相关元素。下面是使用Android Studio修改标题栏的步骤。 1.打开Android Studio,打开项目或者创建一个新项目,并在布局XML文件定位到标题栏(通常是在布局文件的顶部)。 2.在标题添加一个TextView元素,或者添加其他适当的元素以实现你所需的功能。如果想要显示应用程序名称,可以添加一个TextView并设置相应的文本。 3.更改标题栏的外观和风格,比如修改颜色、字体、大小等。可以在XML文件设置标题栏的属性或者使用样式表。 4.使用Java代码实现标题栏的功能,比如实现一个可点击的返回按钮,或者添加一个选项菜单。可以在Java代码处理这些事件,或者使用其他相关类库来处理。 5.对于应用程序的不同页面或活动,可以使用不同的标题栏,以满足特定的需求。可以在每个活动的布局文件设置不同的标题栏,或者使用其他技术来实现标题栏的定制。 总之,Android Studio提供了强大的工具和技术来修改标题栏,以实现应用程序的不同需求。通过深入理解这些技术和工具,可以创建出功能强大、外观精美的应用程序,来吸引和满足用户的需求。 ### 回答3: 在Android应用程序标题栏是一个重要的界面元素,它通常包含应用程序的名称和控制菜单。Android Studio是一个非常流行的Android应用程序开发工具,它允许开发人员以一种直观的方式构建应用程序的界面和功能。如果您是一个Android Studio开发人员,并且想要修改您的应用程序的标题栏,这里是一些简单的步骤。 首先,打开您的Android Studio项目,并找到您的AndroidManifest.xml文件。这个文件位于您应用程序项目的根目录下。在这个文件,找到并打开标记<application>。在这个标记下添加属性android:label,并在该属性输入您想要在标题显示的新名称。 <application android:icon="@drawable/app_icon" android:label="My New App Title"> ... </application> 接下来,打开您的应用程序的主活动(MainActivity)的布局文件。在其,找到并打开AppCompatActivity AppCompatActivity或ActionBarActivity标记,并将其替换为Toolbar标记。Toolbar是一个Android的一个新的界面元素,它提供了更强大和灵活的特性,可以让您更轻松地自定义您的标题栏。 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" /> ... </RelativeLayout> 在Toolbar添加自定义标记和属性,以便您可以更好地满足您的需求。例如,您可以添加一个或多个MenuItem作为菜单按钮,或使用app:title属性来设置标题栏的文本。 <Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" app:title="My New App Title" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" > <ImageButton android:id="@+id/toolbar_menu_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:src="@drawable/menu_icon" android:background="?attr/selectableItemBackgroundBorderless" android:padding="10dp" android:layout_gravity="start" /> <TextView android:id="@+id/toolbar_sub_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/my_subtitle" android:layout_gravity="center" android:layout_marginLeft="48dp" android:textStyle="italic" /> </Toolbar> 总之,在Android Studio修改标题栏是非常简单的。通过这些简单的步骤,您可以很轻松地掌控您的应用程序标题栏的外观和功能。无论你是初学者还是有经验的Android开发人员,你都可以使用这些步骤来改善你的用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值