【问题标题】:Custom Action Bar in Android does not match parentAndroid中的自定义操作栏与父级不匹配
【发布时间】:2015-01-16 20:15:01
【问题描述】:

大家好,我正在尝试制作自定义操作栏。我的代码如下。操作栏右侧的一切都很好,但左侧的自定义操作栏不匹配。我怎么解决这个问题。

感谢您的帮助。

编辑 1: 我的主要活动 xml,它对操作栏没有任何兴趣,但我无法弄清楚问题出在哪里

activity_main.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"              tools:context=".MainActivity"
android:id="@+id/hh">

   </RelativeLayout>

这是我的自定义操作栏 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical" 
      android:layout_width="match_parent"
      android:layout_height="match_parent">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:background="#ffff2301" />
</RelativeLayout>

我的 Java 代码;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setCustomView(R.layout.action_bar);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
}

}

【问题讨论】:

  • 你能上传你的主要活动的xml吗?
  • 而且看起来黑色的空间是菜单按钮的一种空间
  • 感谢您的评论。我已经编辑了我的问题。并且没有发现任何关于操作栏的问题。
  • 请在智能手机的开发设置中打开选项 showLayoutBounda。然后我们可以看到为什么你有这个丑陋的空间;)
  • 再次感谢,但我不知道该怎么做:S 这是模拟器屏幕。

标签: android layout android-actionbar


【解决方案1】:

您在主题提供的默认工具栏上使用setCustomView()。自定义视图旨在加载到被其他视图(徽标、标题、溢出菜单..)占用的工具栏空间中。

因此您的自定义布局成为工具栏的部分,而不是替换它。所以要么:

  1. 你希望事情是这样的。在这种情况下,您的问题只是背景颜色。我不知道您如何将自定义视图设置为黄色,但尝试将android:background="@color/transparent" 添加到RelativeLayout 并将整个工具栏颜色切换为黄色。左边的房间最终会加载导航图标,所以你希望它在那里。

  2. 您想(我建议)使用Toolbar API,它可以更轻松地添加自定义视图。这样做是这样的:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/custom_toolbar"/>
    
    
        <RelativeLayout android:id="@+id/main_content"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    
        </RelativeLayout>
    
    </LinearLayout>
    

然后,在 custom_toolbar.xml 中,

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:abc="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="?attr/actionBarSize"
    android:layout_width="match_parent">

    <!-- you can add any custom view here -->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:background="#ffff2301" />

</android.support.v7.widget.Toolbar>

您现在必须在onCreate() 中致电:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(tb);
    }

    }

【讨论】:

  • 感谢您的评论,第一个 xml 文件将是我的主要活动 xml 吗?
  • 你做得像个魅力兄弟 :) 谢谢。但我想知道。如何设置标题栏中心。
  • 只需添加一个带有android:layout_gravity="center"属性的textview,这个问题已经回答before
猜你喜欢
  • 2015-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-15
  • 2016-10-28
  • 2016-01-15
  • 1970-01-01
相关资源
最近更新 更多