ActionBar Not Showing Up



我是android开发的新手。我想自定义我的应用程序主题。为此,我创建了一个自定义样式。在我的主题中,我希望动作条的颜色不同。我试着做了,但我的动作条看不见了。请帮帮我。代码

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="HsJobTheme" parent="android:Theme.Holo.Light">
        <item name="android:actionBarStyle">@style/MyActionBar</item>

    </style>
    <style name="MyActionBar" parent="android:Theme.Holo.Light.DarkActionBar">
        <item name="android:background">#ffcdc434</item>
    </style>
</resources>

在您的代码中:

ActionBar actionBar=getActionBar();

然后使用:

actionBar.setBackgroundDrawable(R.style.MyActionBar);

创建类似custom_actionbar.xml 的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="50dp"
 android:background="@drawable/black_pattern" >
 <TextView
    android:id="@+id/title_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:textAllCaps="true"
    android:margin="5dp"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#fff"
    android:textStyle="bold" />
</RelativeLayout>

onCreate()的活动中,这样做。。

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

它对我来说很有用

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main1" />

1-set color->android:background="#code color"不要忘记公共类Activity扩展了AppCompatActivity{}2用android工作室或使用此代码

ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR"));

最新更新