[英]Android ActionBar Logo and Title
我正在設置動作欄的樣式。 我想要一種特定的文本顏色,一種特定的背景顏色,並且我希望它在左側具有徽標。 我設法用styles.xml中的這段代碼修改了文本和背景:
<resources>
<style name="MyTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">@style/MyTheme.actionBar</item>
<item name="android:textColorPrimary">#ffffff</item>
</style>
<style name="MyTheme.actionBar" parent="Widget.AppCompat.ActionBar">
<item name="background">@color/actionbar_background</item>
</style>
</resources>
但是,當我在MyTheme.actionBar中添加此代碼時,會出現徽標,但文本會消失。
<item name="logo">@mipmap/giftbox</item>
<item name="displayOptions">useLogo|showHome</item>
我怎樣才能解決這個問題?
編輯:這是活動代碼:
public class Person extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
}
@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_person, 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);
}
}
試試這個代碼
Spannable text = new SpannableString(actionBar.getTitle());
text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
actionBar.setTitle(text);
int titleId = getResources().getIdentifier("action_bar_title", "id",
"android");
TextView title = (TextView) findViewById(titleId);
通過此操作,您可以獲得ActionBar
標題TextView
,可以為其設置自己的文本,顏色,樣式
您可以使用徽標和文本視圖創建自定義標題視圖,並在活動中對其進行充氣。 您也可以使用這種方法為背景設置任何顏色。
就像下面的代碼一樣:
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.top_bar_light)));
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.headerbar_view, null);
imgvwRefrsh = (ImageView) mCustomView.findViewById(R.id.ximgvwrefresh);
imgvwStreamoid = (ImageView) mCustomView.findViewById(R.id.ximgvwStreamoid);
getSupportActionBar().setCustomView(mCustomView);
getSupportActionBar().setDisplayShowCustomEnabled(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.