Android 设置活动被操作栏重叠
我正在从事Android项目,我已经从Android创建了一个新的设置活动。当我创建活动并尝试运行时,它是Android Studio创建的设置活动,由于某种原因没有包含操作栏。我搜索了一下,发现这似乎是一件很常见的事情,并手动添加了操作栏,因此我做了以下工作:Android 设置活动被操作栏重叠,android,Android,我正在从事Android项目,我已经从Android创建了一个新的设置活动。当我创建活动并尝试运行时,它是Android Studio创建的设置活动,由于某种原因没有包含操作栏。我搜索了一下,发现这似乎是一件很常见的事情,并手动添加了操作栏,因此我做了以下工作: private void setupActionBar() { getLayoutInflater().inflate(R.layout.toolbar, (ViewGroup)findViewById(and
private void setupActionBar()
{
getLayoutInflater().inflate(R.layout.toolbar, (ViewGroup)findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
我发现第一个首选项标题隐藏在操作栏下,再次在谷歌上搜索,发现需要在列表视图中添加填充,我在onCreate()中使用了以下命令
执行上述操作似乎有点奇怪,它只在带有首选项列表的初始设置活动屏幕上起作用。单击首选项标题查看设置后,第一个设置将隐藏在操作栏下,如下面的屏幕截图所示:
我建议删除您的操作栏,创建一个自定义工具栏,该工具栏将由其他布局文件描述,并包含一个取消当前活动的小部件 例如:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relLayout1">
<include layout="@layout/snippet_comments_toolbar"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relLayout2"
android:layout_below="@+id/relLayout1"
android:layout_marginBottom="60dp">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listView"></ListView>
</RelativeLayout>
然后是片段注释工具栏,这就是我所说的工具栏:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/white_grey_border_bottom"
>
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/profileToolBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginEnd="20dp"
android:layout_centerVertical="true"
android:id="@+id/backArrow"
android:src="@drawable/ic_backarrow"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:layout_toRightOf="@+id/backArrow"
android:layout_marginRight="15dp"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_centerVertical="true"
android:id="@+id/tvNext"
/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</merge>
我想我找到了答案 我创建了一个工具栏XML,如下所示
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/settings_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
您可以发布布局文件吗?与其以编程方式添加操作栏,不如使用样式进行设置。这样,您的设计就不会中断。另外,你使用的填充物看起来很粗糙。它会破坏你的设计。@Abhi它也让我感觉很不舒服,就像其他一些帖子上说的那样。如何在样式中进行设置,似乎没有setContentView,只有用于首选项标题的XML文件和用于设置的XML文件。这些类和XML是由AndroidStudio自动生成的。Min SDK是15@Boardy我本来打算添加一个答案,但AdamSmith的答案是Android Studio创建动作栏的默认实现。它可以很容易地使用,然后使用
styles.xml
设置样式。您不必使用此方法设置填充。你试过了吗?@Abhi它看起来不是默认的android studio实现。我右键单击了项目添加活动>设置活动,但根本没有操作栏
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/settings_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
private void setupActionBar()
{
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root);
View view = getLayoutInflater().inflate(R.layout.settings_toolbar, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.settings_toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}