簡體   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