简体   繁体   English

在应用程序(Android)中设置导航栏/操作栏颜色

[英]Set Navigation Bar / Action Bar color from within the App (Android)

I have set up an Activity with one EditText and one Button and I'm trying do the following thing: the user types a color in hexadecimal in the EditText, presses the Button and then the NavBar/ActionBar changes its color to the typed value. 我设置了一个带有一个EditText和一个Button的Activity,然后尝试做以下事情:用户在EditText中以十六进制键入一种颜色,按下Button,然后NavBar / ActionBar将其颜色更改为键入的值。

The only way for changing those within the app that I found was to change the Style of the NavBar/ActionBar to a premade one, but I'd like to be able to set it to a custom color. 更改我发现的应用程序中的样式的唯一方法是将NavBar / ActionBar的样式更改为预制样式,但我希望能够将其设置为自定义颜色。

public class VisualTweaks extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_visual_tweaks);

        Button colorButton = (Button) findViewById(R.id.countButton);
        EditText colorEditText = (EditText) findViewById(R.id.colorEditText);

        colorButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (android.os.Build.VERSION.SDK_INT >= 21)
            getWindow().setNavigationBarColor(getResources().getColor(R.color.navigationbar_color));
    }
}

This is a quick fix that i found for the actionBar : 这是我为actionBar找到的快速修复:

getActionBar().setBackgroundDrawable(new ColorDrawable(0xff00DDED));
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowTitleEnabled(true);
ActionBar actionBar;

 actionBar = getActionBar(); 
 ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#93E9FA"));     
 actionBar.setBackgroundDrawable(colorDrawable);

as u same colorDrawable use for your navigation bar also 与您的导航栏相同的colorDrawable用途

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

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