Android自定义操作栏

时间:2015-02-02 07:25:37

标签: android android-actionbar

我遇到的问题是我在应用中添加自定义操作栏,但操作栏视图有一个间隙(红色圆圈位置,在链接的图片中)。

enter image description here

MainActivity代码:

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代码:

<?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>

但是不使用android支持库,这个问题不会发生。(“MainActivity extands ActionBarActivity”更改为“MainActivity extands Activity”,“getSupportActionBar()更改为getActionBar()”)

2 个答案:

答案 0 :(得分:0)

使用style.xml设置Actionbar背景

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

答案 1 :(得分:0)

让我建议另一种方法: 步骤1: 隐藏您的操作栏。 将此主题用于活动。

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

步骤2:      使用自定义相对布局作为操作栏

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