Android 如何在片段中实现自定义工具栏
我读了这么多文章,看了这么多视频,但我仍然不知道,如何在片段中设置动作栏。我有一个带有自定义工具栏的活动X,左上角有一个导航视图(工具栏在xml文件中分开),我将其导入活动X的xml中 这是一个xml中的工具栏:Android 如何在片段中实现自定义工具栏,android,android-fragments,Android,Android Fragments,我读了这么多文章,看了这么多视频,但我仍然不知道,如何在片段中设置动作栏。我有一个带有自定义工具栏的活动X,左上角有一个导航视图(工具栏在xml文件中分开),我将其导入活动X的xml中 这是一个xml中的工具栏: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/a
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerChooser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/new_gradients"
android:fitsSystemWindows="true"
tools:context=".ChoosingActivity">
<!--tools:openDrawer="start"-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_container_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
编辑,新建活动X(查看我的注释“工具栏\选择器或工具栏”):
这里是我的片段,我想在其中添加带有导航视图的工具栏:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class Ausstehende_Treffen_Fragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ausstehende_treffen,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar(toolbar); // HOW???
}
}
编辑,新片段:
public class Ausstehende_Treffen_Fragment extends Fragment {
// Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbarreal);
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_ausstehende_treffen,container,false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Toolbar toolbar = ((ChoosingActivity)getActivity()).findViewById(R.id.toolbar_ausstehende_treffen);
}
}
下面是我的片段xml,我想在其中使用工具栏:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_light">
<include
android:id="@+id/toolbar_ausstehende_treffen"
layout= "@layout/toolbar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ausstehende Treffen Fragment"
android:textSize="25sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
我再也不能在我的选择活动中打开导航栏了,我不知道为什么
我在xml文件中手动包含工具栏,这是必要的吗?+片段本身中的一些代码。伙计,我希望你理解我的代码和我的问题。请帮助我,我只想保持我的自定义工具栏在任何片段和活动
请帮助我,这太令人沮丧了,只为一个已经存在的工具栏浪费了这么多时间。尝试在您的活动中添加工具栏:
mTopToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(mTopToolbar);
获取拥有片段的活动(fragment.getActivity())并设置其ActionBar属性
然后,在将工具栏设置为ActionBar之后,使用与您在ActionBar上提到的相同的setDisplayHomeAsUpEnabled方法,以获取后退/向上按钮
从片段访问它的示例:
((AppCompatActivity)getActivity()).getSupportActionBar().setSubtitle();
希望有帮助。就这么简单
Toolbar toolbar = ((ChoosingActivity)getActivity()).findViewById(R.id.toolbar);
现在做你想做的事,伙计 这可能是因为您的
工具栏
被抽屉布局
中的所有内容所覆盖。尝试交换LinearLayout
和DrawerLayout
,以便垂直LinearLayout
是您的根布局,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerChooser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/new_gradients"
android:fitsSystemWindows="true"
tools:context=".ChoosingActivity">
<FrameLayout
android:id="@+id/fragment_container_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>
</LinearLayout>
大概是这样的,关键是你的
工具栏
必须在你的抽屉布局之外
所以你只想让你活动中的工具栏在你的片段中可见?是的,我自己的,在我的活动中也可见。我想他已经完全按照你的建议做了。如果他想在整个应用程序中保持工具栏,我想他不必经历所有这些麻烦。我想在整个应用程序中显示我的自定义工具栏,除了一个活动(启动活动)。我像这样更改了我的工具栏xml,我用线性布局更改了顶部的抽屉布局。在那之后,我仍然需要线性布局,否则什么也没有显示。但在我的片段上仍然没有带有导航抽屉的custum工具栏。我的片段中的代码是否正确?@M.Hallweger,而不是将工具栏包装在LinearLayout
中,而是尝试将其包装在AppBarLayout
中。编辑您的问题并放入新的XML,我将尝试重新创建问题。我添加了一些新代码。如果你还需要一些,请告诉我。这里还有一些用于关闭和打开的字符串:导航抽屉示例打开导航抽屉关闭导航抽屉@M.Hallweger谢谢您的代码:)重新创建问题有点费时,但是,我做了一个小调查,发现使用了与开始时相同的布局,但是框架布局
位于线性布局
内,请尝试,并尝试将其与您的进行比较,然后告诉我它是如何运行的谢谢,但我需要保留旧版本的xml,否则我无法将所有内容保持在正确的位置,无法单击导航抽屉符号。我认为我的片段中的代码有问题。我必须复制到哪里?我看了这么多次这条线,它不会为我工作。请告诉我我需要改变什么。
((AppCompatActivity)getActivity()).getSupportActionBar().setSubtitle();
Toolbar toolbar = ((ChoosingActivity)getActivity()).findViewById(R.id.toolbar);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerChooser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/new_gradients"
android:fitsSystemWindows="true"
tools:context=".ChoosingActivity">
<FrameLayout
android:id="@+id/fragment_container_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer_menu">
</android.support.design.widget.NavigationView>
</LinearLayout>