2015-06-18 3 views
0

Я устраиваю свою панель действий. Я хочу иметь определенный цвет текста, определенный цвет фона, и я хочу, чтобы у него был логотип слева. Мне удалось изменить текст и фон с этим кодом в styles.xml:Android ActionBar Логотип и название

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

Как это исправить?

EDIT: Вот код активность:

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); 
} 
} 
+0

пытаются использовать [панель инструментов] (http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre .html) вместо панели действий – kId

+0

Plase также опубликовать код деятельности, в котором вы хотите –

+0

см. https://developer.android.com/training/basics/actionbar/styling.html и указать логотип в манифесте tag – Harry

ответ

0

Попробуйте этот код

Spannable text = new SpannableString(actionBar.getTitle()); 
text.setSpan(new ForegroundColorSpan(Color.BLUE), 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); 
actionBar.setTitle(text); 
+0

И как мне получить actionBar? – Alan

+0

ActionBar actionBar = getSupportActionBar(); возвращает null – Alan

+0

requestWindowFeature (Window.FEATURE_NO_TITLE); если вы используете этот код, это означает его удаление. – sasikumar

0
int titleId = getResources().getIdentifier("action_bar_title", "id", 
       "android"); 
TextView title = (TextView) findViewById(titleId); 

этим вы можете получить ActionBar название TextView, для которого вы можете установить свой собственный текст, цвет, стиль

0

Вы можете создать пользовательский заголовок с логотипом и 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); 
Смежные вопросы