简体   繁体   中英

Android custom action bar

I have a problem that is I add the custom action bar in app, but the action bar view has a gap(the red circle position, in the link's picture).

在此处输入图片说明

MainActivity code:

public class MainActivity extends ActionBarActivity {

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

        getSupportActionBar().setCustomView(R.layout.actionbar_layout);
        getSupportActionBar().setDisplayShowCustomEnabled(true);

    }
}

actionbar_layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0146A3">
    <TextView
        android:id="@+id/abr_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:singleLine="true"
        android:textColor="#FFFFFF"
        android:textSize="26sp"
        android:text="Title"
        tools:text="Title"/>
</RelativeLayout>

But not use android support library, this problem does not occur.("MainActivity extands ActionBarActivity" change to "MainActivity extands Activity", and "getSupportActionBar() change to getActionBar()")

set Actionbar background using style.xml

<style name="AppTheme" parent="AppBaseTheme">
        <item name="android:actionBarItemBackground">@drawable/bg_actionbar_selected</item>

</style>


<style name="action_bar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/yourBackgound or color</item>
</style>

Let me suggest another way to do that: Step 1: Hide your actionbar. Use this theme for the activity.

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

step2: Use a custom relative layout as actionbar

    <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="60dp"
                    android:background="#009ACD">
           </RelativeLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM