【问题标题】:set Label Alignment in navigation Fragments在导航片段中设置标签对齐
【发布时间】:2021-04-24 07:50:17
【问题描述】:

我希望标签(工具栏标题)居中对齐。我应该怎么做?我参考了一些逻辑,但没有发现它们有效,它们也不起作用。所以请指导我完成它。

这是导航文件:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="@id/current_weather_fragment">
<fragment
    android:id="@+id/current_weather_fragment"
    android:name="com.example.wheatherapp.UI.Weather.CurrentWeather.CurrentWeatherFragment"
    android:label="current_weather_fragment"

    tools:layout="@layout/current_weather_fragment" />
<fragment
    android:id="@+id/future_weather_fragment"
    android:name="com.example.wheatherapp.UI.Weather.FutureWeather.FutureWeatherListFrag.FutureWeatherFragment"
    android:label="future_weather_fragment"
    tools:layout="@layout/future_weather_fragment" />
<fragment
    android:id="@+id/future_detail_weather"
    android:name="com.example.wheatherapp.UI.Weather.FutureWeather.FutureDetailFrag.FutureDetailWeather"
    android:label="future_detail_weather_fragment"
    tools:layout="@layout/future_detail_weather_fragment" />
<fragment
    android:id="@+id/setting_fragment"
    android:name="com.example.wheatherapp.UI.Setting.SettingFragment"
    android:label="SettingFragment" />
</navigation>

这是 MainActvity.xml

<?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.MainActivity">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    android:theme="@style/ToolbarTheme">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary"
        />
</com.google.android.material.appbar.AppBarLayout>


<androidx.fragment.app.FragmentContainerView
    android:id="@+id/nav_host_frag"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/appbar_layout"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@id/bottom_nav"
    app:navGraph="@navigation/mobile_navigation"
    app:defaultNavHost="true"
    android:name="androidx.navigation.fragment.NavHostFragment" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:menu="@menu/bottom_nav_menu"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>

这是我希望标题居中对齐的图像: the uploaded one

Style.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.WheatherApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_200</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_200</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="ToolbarTheme" parent="ThemeOverlay.MaterialComponents.ActionBar">
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:textColorSecondary">#EEEEEE</item>
    <item name="android:gravity">center_vertical|center_horizontal</item>
    <item name="background">?colorPrimary</item>
</style>
</resources>

【问题讨论】:

    标签: java android xml android-studio kotlin


    【解决方案1】:

    1) 将工具栏标题的 Textview 添加到您的标签中,该标签位于您的案例中的 MainActivity.xml 中。

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:theme="@style/ToolbarTheme">
    
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?colorPrimary">
    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Borrowers"
        android:textColor="@color/white"
        android:textSize="18dp" />
    </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>
    

    2) 在 onCreate() 方法中将工具栏添加到您的 MainActivity.java

    //set toolbar
        Toolbar toolbar = findViewById(R.id.borrowerstoolbar_main);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多