繁体   English   中英

如何将自定义工具栏放在片段中?

[英]How to put custom toolbar inside fragment?

我在片段中创建了自定义工具栏,但我得到了这样的视图: 工具栏

在themes.xml中我禁用了ActionBar:

<style name="Theme.Project" parent="Theme.MaterialComponents.DayNight.NoActionBar">

这是我的自定义工具栏布局:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/white"
            >

            <TextView
                android:id="@+id/toolbar_title"
                style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Home"
                android:fontFamily="@font/google_sans"
                android:textColor="@color/black"
                android:textSize="20sp" />

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/fragment_home" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

这是 HomeFragment.java:

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.toolbar_in_home, container, false);

        return root;
    }
}

如何去除屏幕顶部的这个白色东西?

升级版:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.home.HomeFragment">


</androidx.constraintlayout.widget.ConstraintLayout>

这是片段主页(只是 ConstraintLayout)

UPD2:我也尝试在 activity_main 中使用工具栏,它可以工作:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.toolbar);
        BottomNavigationView navView = findViewById(R.id.nav_view);


        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_examination, R.id.navigation_profile)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(navView, navController);
    }

主屏幕

但是,如果我想在其他片段上更改 go,我将无法进行更改。 我需要使用这样的按钮设置另一个自定义工具栏布局:

在此处输入图像描述

所以我就是这样做的。 我的应用程序基于单活动多片段 model。

我已将活动主题设置为AppTheme.NoActionBar ,因此它使我能够设置自定义工具栏。

后来对于每个片段,我都有一个单独的布局,并在其中添加了一个自定义工具栏,如下所示

<androidx.appcompat.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary">
....
....
</androidx.appcompat.widget.Toolbar>

在这个布局中,我添加了菜单项。

如果你不想遵循这个,可能你应该看看getActionBar().setCustomView() 保持活动主题设置,因此getActionBar()不是 null。

您可以通过在片段 onResume() 中调用getActionBar().setCustomView()来控制操作栏布局,或者可能在您在活动中添加或删除片段时。

您可以在主布局中使用自定义工具栏将其从com.google.android.material.appbar.AppBarLayout替换。

这是一个示例代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:background="@color/colorPrimary">

        <ImageView
            android:id="@+id/navi"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_alignParentStart="true"
            android:layout_centerInParent="true"
            android:layout_marginStart="20dp"
            android:contentDescription="@string/todo"
            android:src="@drawable/navi" />

        <ImageView
            android:layout_width="180dp"
            android:layout_height="?attr/actionBarSize"
            android:layout_alignParentTop="true"
            android:layout_centerInParent="true"
            android:contentDescription="@string/todo"
            android:src="@drawable/header2" />
    </RelativeLayout>
 <include layout="@layout/fragment_home" />
</RelativeLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM