2015-05-04 1 views
1

У меня возникают проблемы с панелью действий. Я создал новый макет для моей настраиваемой панели действий и применил его. Пользовательская панель действий отлично работает, но при запуске приложения сначала появляется панель действий по умолчанию, а затем она исчезает. Я исследовал, но я не мог найти решение проблемы.Хотя я использую пользовательскую панель действий, панель действий по умолчанию по-прежнему выглядит

Кода моей основной деятельности, как следующие ..

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    ActionBar myActionbar = getActionBar(); 
    myActionbar.setCustomView(R.layout.custom_actionbar); 
    myActionbar.setDisplayShowHomeEnabled(false); 
    myActionbar.setDisplayShowTitleEnabled(false); 
    myActionbar.setDisplayShowCustomEnabled(true); 


} 

и styles.xml

<resources> 

<style name="appTheme" 
    parent="@android:style/Theme.Holo.Light"> 
</style> 

</resources> 
+0

Привет, можете ли вы рассказать мне, что содержит пользовательский макет для панели действий? – luckylukein

ответ

0

Добавьте следующий код в файле styles.xml в вашем Резе/значениях папки

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style name="Theme.Default" parent="@android:style/Theme"></style> 
    <style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style> 
    <style name="Theme.FullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style> 
</resources> 

Добавьте следующий код в свой файл styles.xml в папку res/values-v11

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style name="Theme.Default" parent="@android:style/Theme.Holo"></style> 
    <style name="Theme.NoTitle" parent="@android:style/Theme.Holo.NoActionBar"></style> 
    <style name="Theme.FullScreen" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen"></style> 
</resources> 

Добавьте следующий код в файл styles.xml в папку Рез/значения-V14

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
    <style name="Theme.Default" parent="@android:style/Theme.Holo.Light"></style> 
    <style name="Theme.NoTitle" parent="@android:style/Theme.Holo.Light.NoActionBar"></style> 
    <style name="Theme.FullScreen" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></style> 
</resources> 

Наконец, в файле AndroidManifest.xml добавить следующий код в деятельности теге деятельности вы не хотите заголовка или тега приложения, если вы хотите, чтобы он применим ко всему приложению.

android:theme="@style/Theme.NoTitle" 

Надеюсь, это поможет!

+0

Во-первых, спасибо за ваш ответ. Когда я добавляю NoTitleBar в AndroidManifest.xml, я беру исключение NullPointerException из-за myActionbar.setCustomView (R.layout.custom_actionbar); – Developer

+0

Какова версия min-sdk в файле манифеста Android? И вы можете проверить использование метода getSupportActionBar() вместо метода getActionBar(). –

+0

Попробуйте добавить следующую строку 'getWindow(). RequestFeature (Window.FEATURE_ACTION_BAR); 'перед setContentView() –

0

Вот пример ..

Сначала вы должны создать макет для бара пользовательских действий с целью определить функциональные возможности в соответствии с вашими требованиями. Вот XML-файл ...

расположение для пользовательских действий бар:

<ImageView 
     android:id="@+id/custom_actionbar_back_iv" 
     android:layout_width="@dimen/small_margin_ar" 
     android:layout_height="@dimen/very_small_margin_ar" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/up_navigation"/> 

    <TextView 
     android:id="@+id/custom_actionbar_titleText_tv" 
     style="@style/wrapscreen" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/custom_actionbar_back_iv" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="@color/white" 
     android:textSize="@dimen/above_medium_text_size" /> 

    <ImageButton 
     android:id="@+id/custom_actionbar_goToArchiveActivity_ib" 
     style="@style/wrapscreen" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="@dimen/medium_margin" 
     android:background="@null" 
     android:src="@drawable/ic_action_overflow" /> 

Здесь идет осуществление ..

Определение setupactionBar() метод в OnCreate метод()

private void setUpActionBar() { 
     ActionBar actionBar = getActionBar(); 
     actionBar.setDisplayShowHomeEnabled(false); 
     actionBar.setDisplayShowTitleEnabled(false); 

     LayoutInflater layoutInflater = LayoutInflater.from(this); 
     View customActionBarView = layoutInflater.inflate(R.layout.custom_actionbar, null); 

     // this view is used to show tile 
     TextView ActivityTitleTV = (TextView) customActionBarView.findViewById(R.id.custom_actionbar_titleText_tv); 
     ActivityTitleTV.setText(R.string.title_text_archive); 

     //this view can be used for back navigation 
     ImageView backToRecorderIV = (ImageView) customActionBarView.findViewById(R.id.custom_actionbar_back_iv); 
     backToRecorderIV.setVisibility(View.VISIBLE); 
     backToRecorderIV.setOnClickListener(this); 

     //Another view which has up navigation 
     ImageButton goToArchiveActivityIB = (ImageButton) customActionBarView.findViewById(R.id.custom_actionbar_goToArchiveActivity_ib); 
     goToArchiveActivityIB.setVisibility(View.GONE); 

     actionBar.setCustomView(customActionBarView); 
     actionBar.setDisplayShowCustomEnabled(true); 
    } 

    //listener for back navigation 
    @Override 
    public void onClick(View view) { 
     switch (view.getId()) { 
      case R.id.custom_actionbar_back_iv: 
       Intent recorder = new Intent(ArchiveActivity.this, RecorderActivity.class); 
       startActivity(recorder); 
       finish(); 
       break; 
     } 
    } 

Надеюсь, это поможет ...

0

Я сделал следующее для настраиваемой панели действий. Может быть, som ему это нужно.

Я создал custom_actionbar_theme.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<style 
name="CustomActionBarTheme" 
parent="@style/Theme.AppCompat.Light.DarkActionBar"> 
<item name="actionBarStyle">@style/CustomActionBarStyle</item> 
</style> 

<style 
name="CustomActionBarStyle" 
parent="@style/Widget.AppCompat.ActionBar"> 
<item name="background">@drawable/action_bar_background</item> 
<item name="titleTextStyle">@style/ActionBarTextColor</item> 
</style> 


<style name="ActionBarTextColor" 
    parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"> 
    <item name="android:textColor">@color/actionBarTitleColor</item> 
</style> 


</resources> 

Я изменил фон и цвет заголовка в панели действий в этом XML. Затем я изменил

<application 
     ... 
     android:theme="@style/CustomActionBarTheme" > 
Смежные вопросы