繁体   English   中英

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

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

我在我的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>

我有一个Action Bar.xml,我想添加这种样式:

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

如您所见,带有操作栏的xml文件现在为空,我无法确定如何向其中添加样式。

我的目标是创建具有不同背景颜色的自定义操作栏,该怎么办?

这将为您工作。 您可以在colors.xml中更改工具栏的颜色。 colorPrimary用于工具栏,colorPrimaryDark用于状态栏

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

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

<?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

<!-- 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" />

我建议以下。 您需要确保gradle.build文件中具有AppCompat库依赖gradle.build (即, compile 'com.android.support:appcompat-v7:23.1.1' ,示例gradle文件在此处

首先,确保您的应用程序主题具有Theme.AppCompat.*父级。 然后,为您的操作栏分配带有首选颜色的colorPrimary项目。 例如,在您的styles.xml

<resources>

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

</resources>

第二,确保AppThememanifest您应用程序的主题。

最后,您的Activity类将需要扩展AppCompatActivity而不是Activity

只需添加:

android:theme="@styles/LoginMenu"

暂无
暂无

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

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