简体   繁体   English

将自定义样式设置为Action Bar.xml

[英]Setting a custom Style to an Action Bar.xml

I created a custom style for my action bar on my android project: 我在我的android项目上为操作栏创建了自定义样式:

<!-- Style for the Login Menu Toolbar -->
<style name="LoginMenu">
    <item name="android:colorBackground">@color/menu</item>
    <item name="android:textColor">@color/menuFont</item>
</style>

And I have a Action Bar.xml which I want to add this style: 我有一个Action Bar.xml,我想添加这种样式:

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
</menu>

As you can see, the xml file with the action bar is empty right now, and I can't figure how to add the style to it... 如您所见,带有操作栏的xml文件现在为空,我无法确定如何向其中添加样式。

My objective is to create a custom Action Bar with different background colors, how should I do this? 我的目标是创建具有不同背景颜色的自定义操作栏,该怎么办?

This will work for you. 这将为您工作。 You can change the colors of toolbar in colors.xml. 您可以在colors.xml中更改工具栏的颜色。 colorPrimary is for ToolBar and colorPrimaryDark is for status bar colorPrimary用于工具栏,colorPrimaryDark用于状态栏

activity_main.xml activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="com.newapp.toolbarwork.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.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" />

    </android.support.design.widget.AppBarLayout>

   <!-- <include layout="@layout/content_main" />-->



</android.support.design.widget.CoordinatorLayout>

MainActivity.java MainActivity.java

package com.newapp.toolbarwork;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

colors.xml colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
</resources>

styles.xml styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

I would recommend the following. 我建议以下。 You will need to ensure you have the AppCompat library dependency in your gradle.build file (ie compile 'com.android.support:appcompat-v7:23.1.1' , an example gradle file is here ) 您需要确保gradle.build文件中具有AppCompat库依赖gradle.build (即, compile 'com.android.support:appcompat-v7:23.1.1' ,示例gradle文件在此处

First, ensure your app's theme has a Theme.AppCompat.* parent. 首先,确保您的应用程序主题具有Theme.AppCompat.*父级。 Then, assign colorPrimary item with the preferred colour for your action bar. 然后,为您的操作栏分配带有首选颜色的colorPrimary项目。 For example, in your styles.xml : 例如,在您的styles.xml

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/my_action_bar_color</item>
    </style>

</resources>

Second, ensure that AppTheme is your application's theme in your manifest . 第二,确保AppThememanifest您应用程序的主题。

Finally, your Activity classes will need to extend AppCompatActivity rather than Activity . 最后,您的Activity类将需要扩展AppCompatActivity而不是Activity

只需添加:

android:theme="@styles/LoginMenu"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM