android中的自定义操作栏首先显示默认操作栏

时间:2014-11-04 11:00:13

标签: android android-actionbar

我是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);

我认为肯定会有一些愚蠢的错误 请帮忙。

2 个答案:

答案 0 :(得分: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 :(得分: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>