2014-11-04 52 views
1

我是android新手,而自定義動作欄我注意到,首先它首先顯示舊的(默認)動作欄,然後顯示自定義動作欄。另外我想爲操作欄標題設置文本顏色。 我的代碼段是在android中自定義動作欄首先顯示默認動作欄

public class MainActivity extends ActionBarActivity { 
    MenuItem searchItem; 
    SearchView searchView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ActionBar ab = getSupportActionBar(); 

     ab.setTitle("Modification"); ab.setIcon(R.drawable.ap); 
     ab.setBackgroundDrawable(new 
     ColorDrawable(Color.parseColor("#00AA00"))); 
     ab.setSubtitle("Modification"); 

     setContentView(R.layout.activity_main); 

我認爲必須有一些愚蠢的錯誤。
請幫忙。

回答

1

試試這個..
創建theme.xml作爲如下

<resources> 
<!-- the theme applied to the application or activity --> 
<style name="CustomActivityTheme" parent="@android:style/Theme.Holo"> 
    <item name="android:actionBarStyle">@style/MyActionBar</item> 
    <!-- other activity and action bar styles here --> 
</style> 
<!-- style for the action bar backgrounds --> 
<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> 
    <item name="android:background">#5477B1</item> 
</style> 

然後在你的AndroidManifest.xml 提到這應用標籤

<application 
    .. 
    android:theme="@style/CustomActionBarTheme" 

好運

+1

謝謝。有用。 – 2014-11-04 12:07:34

+0

@Ichigo Kurosaki 大家好我有同樣的問題在這裏看到你可以請點擊這裏查看 http://stackoverflow.com/q/26716029/3255471 – Biftor 2014-11-04 17:24:26

0

嘗試這樣,

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

     ActionBar mActionBar = getActionBar(); 
     mActionBar.setDisplayShowHomeEnabled(false); 
     mActionBar.setDisplayShowTitleEnabled(false); 
     LayoutInflater mInflater = LayoutInflater.from(this); 

     View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 
     TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text); 
     mTitleTextView.setText("My Own Title"); 

     mActionBar.setCustomView(mCustomView); 
     mActionBar.setDisplayShowCustomEnabled(true); 
    } 

或 的操作欄創建樣式和使用自定義背景:

<resources> 
    <!-- the theme applied to the application or activity --> 
    <style name="CustomActivityTheme" parent="@android:style/Theme.Holo"> 
     <item name="android:actionBarStyle">@style/MyActionBar</item> 
     <!-- other activity and action bar styles here --> 
    </style> 
    <!-- style for the action bar backgrounds --> 
    <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> 
     <item name="android:background">@drawable/background</item> 
     <item name="android:backgroundStacked">@drawable/background</item> 
     <item name="android:backgroundSplit">@drawable/split_background</item> 
    </style> 
</resources> 
+0

您好,我們已經嘗試你的方法,但這並沒有解決我的問題。請向我們提供一些其他方法來解決此問題。 – 2014-11-04 11:44:54

+0

@Moradiya Akash 大家好我有同樣的問題在這裏看到你可以請檢查在這裏 http://stackoverflow.com/q/26716029/3255471 – Biftor 2014-11-04 17:24:48