首页 > 解决方案 > 我应该在我的依赖项中添加什么来制作工具栏?

问题描述

我想删除操作栏并用工具栏替换,但我不知道要为我的依赖项添加什么!这是我的 gradle.build 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.helloword"
        minSdkVersion 14
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

标签: javaandroidandroid-studio

解决方案


您可以将此主题添加到您的样式中:

<style name="MainTheme" parent="Theme.AppCompat.Light.NoActionBar">
   <item name="colorPrimary">@color/colorPrimary</item>
   <item name="colorPrimaryDark">@color/loginBackground</item>
   <item name="colorAccent">@color/colorAccent</item>
</style>

将此添加到 AndroidManifest.xml 中的活动主题

<activity android:name=".YourActivity"
          android:theme="@style/MainTheme" />

现在您可以将工具栏添加到您的活动 xml 文件中

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

现在终于将它添加到您的活动中

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

推荐阅读