简体   繁体   English

Android自定义操作栏

[英]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: 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 code: 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>

But not use android support library, this problem does not occur.("MainActivity extands ActionBarActivity" change to "MainActivity extands Activity", and "getSupportActionBar() change to getActionBar()") 但不使用android支持库,则不会发生此问题。(“ MainActivity extands ActionBarActivity”更改为“ MainActivity extands Activity”,而“ getSupportActionBar()更改为getActionBar()”)

set Actionbar background using style.xml 使用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. 让我建议另一种方法:步骤1:隐藏操作栏。 Use this theme for the activity. 使用此主题进行活动。

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

step2: Use a custom relative layout as actionbar 步骤2:使用自定义相对布局作为操作栏

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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