【问题标题】:Android custom action barAndroid 自定义操作栏
【发布时间】:2015-02-02 16:33:10
【问题描述】:

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

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()”)

【问题讨论】:

标签: android android-actionbar


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    第二步: 使用自定义相对布局作为操作栏

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

    【讨论】:

    • 能否请您在这里提及错误。我已经检查过,没有错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多