大家好,我正在尝试制作自定义操作栏。我的代码如下。操作栏右侧的一切都很好,但左侧的自定义操作栏不匹配。我怎么解决这个问题。
感谢您的帮助。
编辑 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 、标题、溢出菜单...)占用的工具栏空间中。
因此您的自定义布局将成为工具栏的一部分,并且不会取代它。所以要么:
你希望事情变成这样。在这种情况下,您的问题只是背景颜色。我不知道你如何将自定义 View 设置为黄色,但尝试将
android:background="@color/transparent"
添加到RelativeLayout
并切换整个工具栏颜色改为黄色。左侧的房间最终将加载导航图标,因此您希望它在那里。您想要(我建议)使用
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/