Actionbar home, arrow, title color 변경하는 방법입니다.

style 테마로 지정하는 방법도 있겠지만, 지금 경우는 버튼을 눌렀을 경우 유동적으로 변할 수 

있도록 java 코드로 작성하였습니다.


color 값을 준비합니다.

    <color name="color_memo1">#03a9f3</color>
    <color name="color_memo2">#8bc349</color>
    <color name="color_memo3">#ffc108</color>
    <color name="color_memo4">#ff5720</color>
    <color name="color_memo5">#9e9e9e</color>

    <color name="color_text1">#0000ff</color>
    <color name="color_text2">#259b20</color>
    <color name="color_text3">#ff9700</color>
    <color name="color_text4">#ff0000</color>
    <color name="color_text5">#ffffff</color>


코드 내용

private void setColorBt(int index) {

	ib_color_1.setBackgroundResource(R.color.color_memo1);
	ib_color_2.setBackgroundResource(R.color.color_memo2);
	ib_color_3.setBackgroundResource(R.color.color_memo3);
	ib_color_4.setBackgroundResource(R.color.color_memo4);
	ib_color_5.setBackgroundResource(R.color.color_memo5);

	int colorBgId = R.color.color_memo1;
	int colorTxtId = R.color.color_text1;
	if(index == 0) {
		ib_color_1.setBackgroundResource(R.drawable.bt_color_frame);
	} else if(index == 1) {
		colorBgId = R.color.color_memo2;
		colorTxtId = R.color.color_text2;
		ib_color_2.setBackgroundResource(R.drawable.bt_color_frame);
	} else if(index == 2) {
		colorBgId = R.color.color_memo3;
		colorTxtId = R.color.color_text3;
		ib_color_3.setBackgroundResource(R.drawable.bt_color_frame);
	} else if(index == 3) {
		colorBgId = R.color.color_memo4;
		colorTxtId = R.color.color_text4;
		ib_color_4.setBackgroundResource(R.drawable.bt_color_frame);
	} else if(index == 4) {
		colorBgId = R.color.color_memo5;
		colorTxtId = R.color.color_text5;
		ib_color_5.setBackgroundResource(R.drawable.bt_color_frame);
	}
	lay_frame.setBackgroundResource(colorBgId);

	setActionBarColor(colorBgId, colorTxtId);
}

public void setActionBarColor(int colorBgId, int colorId) {

	int colorBg = ContextCompat.getColor(getBaseContext(), colorBgId);
	int colorText = ContextCompat.getColor(getBaseContext(), colorId);

	ActionBar bar = getSupportActionBar();
	if(bar != null) {
		// bar 백그라운드 색상 변경
		bar.setBackgroundDrawable(new ColorDrawable(colorBg));

		// 타이틀 색상 변경
		Spannable text = new SpannableString(bar.getTitle());
		text.setSpan(new ForegroundColorSpan(colorText), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
		bar.setTitle(text);

		// 홈,화살표 버튼 색상 변경
		mToggle.getDrawerArrowDrawable().setColor(colorText);
	}
}


결과 화면

    


+ Recent posts