sourcetip

XML을 사용하여 ActionBar Activity의 ActionBar 배경색을 변경하려면 어떻게 해야 합니까?

fileupload 2023. 8. 21. 21:32
반응형

XML을 사용하여 ActionBar Activity의 ActionBar 배경색을 변경하려면 어떻게 해야 합니까?

세부사항:

Action Bar Activity는 다음과 같습니다.
Eclipse 및 SDK는 2011-11-06 기준으로 완전히 패치되었습니다.

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />  

Android 2.3.3이 설치된 Samsung 기기에 배포됨
응용 프로그램이 있습니다.android:theme="@android:style/Theme.Light"

문제: 응용 프로그램은 밝은 색이지만 작업 표시줄은 회색 아이콘이 있는 파란색이며 파란색 배경색에서는 거의 보이지 않습니다.또한 회색 아이콘이 더 잘 보이도록 작업 표시줄을 밝게 표시합니다.

스타일을 수정해봤지만 소용이 없었습니다.
저는 아마 사소한 것을 놓치고 있을 것입니다.

XML을 사용하여 ActionBar Activity의 ActionBar 배경색을 변경하려면 어떻게 해야 합니까?

설명서에 따라 - "Android 3.0(API 레벨 11)에 추가된 ActionBar API를 사용하여 ActionBar의 동작 및 가시성을 제어할 수 있습니다."

따라서 API 레벨 10(Android 2.3.3)인 대상 환경에서는 ActionBar가 작동하지 않습니다.

만일 최소 API 레벨 11을 목표로 한다면, 다음과 같이 사용자 정의 스타일을 정의하여 ActionBar의 배경색을 변경할 수 있습니다.

<resources>
    <style name="MyTheme" parent="@android:style/Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:background">ANY_HEX_COLOR_CODE</item>
    </style>
</resources>

그리고 "내 테마"를 응용프로그램/활동의 테마로 설정합니다.

사용해 보세요.

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

사용해 보십시오.

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));

사용 - http://jgilfelt.github.io/android-actionbarstylegenerator/

라이브 미리 보기를 사용하여 수행 표시줄을 사용자 지정할 수 있는 놀라운 도구입니다.

저는 이전 답변을 시도했지만 탭과 문자와 같은 다른 색상을 변경하는 데 항상 문제가 있었고 몇 시간 동안 수정했습니다.이 툴을 사용하여 단 몇 분 만에 설계를 완료할 수 있었습니다.

Here's a screenshot of the tool

좋은 일만 가득하세요! :)

저도 같은 문제가 있었습니다. 코드를 입력하면 작업 표시줄이 회색으로 변합니다.원래 스타일시트는 다음과 같습니다.

<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>

"다크 액션 바"는 액션 바를 회색으로 유지하는 것이었습니다.이렇게 바꿨더니 효과가 있었습니다.

<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
    <!-- API 14 theme customizations can go here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
    <item name="android:background">#2aa4cd</item>
    <item name="android:titleTextStyle">@style/Theme.MyAppTheme.ActionBar.TitleTextStyle</item>
</style>        

<style name="Theme.MyAppTheme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textColor">#FFFFFF</item>
</style>    

텍스트 색상을 편집하는 방법도 제출했습니다.또한 리소스와 관련된 내용을 변경할 필요가 없습니다.

-워렌

Actionbar 동작은 API < 11에서 변경될 수 있습니다.

자세한 내용은 Android 공식 설명서를 참조

나는 앱을 만들고 있습니다.minSdkVersion = "9"그리고.targetSdkVersion = "21"액션 바의 색상을 변경했는데 API 레벨 9에서 잘 작동합니다.

여기 xml이 있습니다.

res/values/themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
           parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->
        <item name="actionBarStyle">@style/MyActionBar</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/actionbar_background</item>

        <!-- Support library compatibility -->
        <item name="background">@color/actionbar_background</item>
    </style>
</resources>

원하는 작업 표시줄의 색을 설정합니다.

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="actionbar_background">#fff</color> //write the color you want here
</resources>

수행 표시줄 색상은 다음에서 정의할 수도 있습니다..class파일, 스니펫은

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0000ff")));

그러나 이것API < 11에서 작동하지 않을 이므로, 액션 바를 스타일링합니다.xmlAPI < 11을 위한 유일한 방법입니다.

코틀린 사용자들을 위해 - 해봤는데 이것은 라이트 모드와 다크 모드에서 작동합니다 -

    getSupportActionBar()?.setBackgroundDrawable(
             ColorDrawable(Color.parseColor("#003459"))
        
)

다음과 같이 만들기에 추가 -

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        getSupportActionBar()?.setBackgroundDrawable(
             ColorDrawable(Color.parseColor("#003459"))
        )

Nexus 4 사람들은 이것이 색을 회색으로 만드는 것 같습니다.

ActionBar bar = getActionBar(); // or MainActivity.getInstance().getActionBar()
bar.setBackgroundDrawable(new ColorDrawable(0xff00DDED));
bar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
bar.setDisplayShowTitleEnabled(true);

(이 게시물에 대한 모든 공은 인정하지만, 댓글에 묻혀 있기 때문에 여기에 표면화하고 싶었습니다.) https://stackoverflow.com/a/17198657/1022454

한 줄 코드 사용:

getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MainColor)));

2021년: 코틀린 원라이너, 더 이상 추천되지 않음:

supportActionBar?.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this,R.color.red)))

필요에 따라 색을 만들고 변경하기만 하면 됩니다.

이렇게 수행 표시줄의 색을 변경할 수 있습니다.

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;


public class ActivityUtils {

public static void setActionBarColor(AppCompatActivity appCompatActivity, int colorId){
    ActionBar actionBar = appCompatActivity.getSupportActionBar();
    ColorDrawable colorDrawable = new ColorDrawable(getColor(appCompatActivity, colorId));
    actionBar.setBackgroundDrawable(colorDrawable);
}

public static final int getColor(Context context, int id) {
    final int version = Build.VERSION.SDK_INT;
    if (version >= 23) {
        return ContextCompat.getColor(context, id);
    } else {
        return context.getResources().getColor(id);
    }
}
}

기본 활동.java에서 수행 표시줄 색을 다음과 같이 변경합니다.

    ActivityUtils.setActionBarColor(this, R.color.green_00c1c1);

활동을 확장할 때는 다음 코드를 사용합니다.

ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));

AppCompativity 활동을 확장하는 경우 다음 코드를 사용합니다.

ActionBar actionbar = getSupportActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));

때때로 이 옵션은 NullPointer를 슬로우합니다.예외.

ActionBar actionbar = getActionBar(); actionbar.setBackgroundDrawable(new ColorDrawable("color"));

하지만 이 옵션은 저에게 효과가 있습니다.여러분들도 한번 해보세요.

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFFFFF")));

해피 코딩

AppCompativity의 경우에는 효과가 있었습니다.

    <item name="actionBarStyle">@style/BlackActionBar</item>
</style>

<style name="BlackActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">
    <item name="background">@android:color/black</item>
    <item name="titleTextStyle">@style/BlackActionBarTitleTextStyle</item>
</style>

<style name="BlackActionBarTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@android:color/white</item>
    <item name="android:fontFamily">@font/cinzel_decorative</item>
</style>

Androidx AppCompact를 사용하는 경우.아래 코드를 사용합니다.

androidx.appcompat.app.ActionBar actionBar = getSupportActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable("Color"));

이 코드는 도움이 될 수 있습니다.

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
    <!-- Customize your theme here. -->
</style>
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- customize the color palette -->
    <item name="colorPrimary">@color/material_blue_500</item>
    <item name="colorPrimaryDark">@color/material_blue_700</item>
    <item name="colorAccent">@color/material_blue_500</item>
    <item name="colorControlNormal">@color/black</item>

</style>
<style name="CardViewStyle" parent="CardView.Light">
    <item name="android:state_pressed">@color/material_blue_700</item>
    <item name="android:state_focused">@color/material_blue_700</item>
    <!--<item name="android:background">?android:attr/selectableItemBackground</item>-->
</style>

API 21 이상을 사용하는 사용자에게는 매우 간단합니다. 아래 코드를 사용하십시오.

다크 액션 막대용

<resources>
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:background">@color/colorDarkGrey</item>
</style>

for_LightActionB

<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.ActionBar">
    <item name="android:background">@color/colorDarkGrey</item>
</style>

이 코드를 사용하여 수행 표시줄 배경색을 변경합니다."res/values/sys.xml"(존재하지 않으면 생성)을 열고 추가합니다.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
       parent="@android:style/Theme.Holo.Light.DarkActionBar">
    <item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
 <!-- ActionBar styles -->
<style name="MyActionBar"
       parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@drawable/actionbar_background</item>
</style>

참고: 이 코드는 Android 3.0 이상 버전에서만 작동합니다.

이것을 사용하면 효과가 있을 것입니다.

ActionBar actionbar = getActionBar();
actionbar.setBackgroundDrawable(new ColorDrawable("color"));
getActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.TabColor)));

color.xml 파일:

<resources> <color name="TabColor">#11FF00</color> </resources>`

언급URL : https://stackoverflow.com/questions/8024706/how-do-i-change-the-background-color-of-the-actionbar-of-an-actionbaractivity-us

반응형