android - Android 中的自定义操作栏与父级不匹配

标签 android layout android-actionbar

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

感谢您的帮助。

enter image description here

编辑 1: 我的主要 Activity 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);
}

}

最佳答案

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

因此您的自定义布局将成为工具栏的一部分,并且不会取代它。所以要么:

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

  2. 您想要(我建议)使用 Toolbar API,这样可以更轻松地添加自定义 View 。这是这样完成的:

    <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);
    }

    }

关于android - Android 中的自定义操作栏与父级不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27979106/

相关文章:

java - Android Studio 禁用点击声音

android - 为什么 android 按钮会卡住我的应用程序并且无法运行?

Android getActionBar().setTitle 不起作用

java - 我无法删除工具栏上的图标

android - 单击时如何显示/隐藏操作栏

android - Kitkat 上的 Actionbar 被替换为白色框并带有 AdjustPan

android - 在android中创建带有两个角边的形状

Android 设置铃声仅在第一次时有效

java - 在JAVA中使用多个布局管理器

android - 图库布局问题