Android 中的自定义操作栏与父项不匹配
Custom Action Bar in Android does not match parent
大家好,我正在尝试制作自定义操作栏。我的代码如下。操作栏右侧的一切都很好,但左侧自定义操作栏不匹配。我怎么解决这个问题。
感谢您的帮助。
编辑 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()
。自定义视图应加载到工具栏 space 中,该工具栏 不会 被其他视图(徽标、标题、溢出菜单..)占用。
所以您的自定义布局成为工具栏的部分,而不是替换它。所以要么:
你希望事情是这样的。在这种情况下,您的问题只是背景颜色。我不知道您如何将自定义视图设置为黄色,但请尝试将 android:background="@color/transparent"
添加到 RelativeLayout
并将整个工具栏颜色切换为黄色。左边的房间最终会加载导航图标,所以你希望它在那里。
您想(我建议)使用 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);
}
}
大家好,我正在尝试制作自定义操作栏。我的代码如下。操作栏右侧的一切都很好,但左侧自定义操作栏不匹配。我怎么解决这个问题。
感谢您的帮助。
编辑 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()
。自定义视图应加载到工具栏 space 中,该工具栏 不会 被其他视图(徽标、标题、溢出菜单..)占用。
所以您的自定义布局成为工具栏的部分,而不是替换它。所以要么:
你希望事情是这样的。在这种情况下,您的问题只是背景颜色。我不知道您如何将自定义视图设置为黄色,但请尝试将
android:background="@color/transparent"
添加到RelativeLayout
并将整个工具栏颜色切换为黄色。左边的房间最终会加载导航图标,所以你希望它在那里。您想(我建议)使用
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);
}
}