Android 中导航栏文字居中

Android 中导航栏文字居中

1. 隐藏原来的导航栏

在这里插入图片描述
在这里插入图片描述

        <activity
            android:name=".MainActivity"
            android:label="@string/main_title"
            android:theme="@style/CustomMainTheme" />
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Main 首页的头部样式, 先隐藏后添加 -->
    <style name="CustomMainTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:background">@color/main_header</item>
    </style>

</resources>

2. 添加导航栏

先建立一个布局: layout_main_actionbar.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="40dp">

    <TextView
        android:id="@+id/main_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/main_title"
        android:textColor="@color/white" />

</RelativeLayout>

嵌入这个布局

在这里插入图片描述

是标题居中

在这里插入图片描述

        // 标题栏居中
        TextView tv_title = findViewById(R.id.main_title);
        tv_title.setGravity(Gravity.CENTER);
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
微信小程序导航栏的文字可以通过设置`navigationBarTitleText`属性来显示,但默认情况下,文字是左对齐显示的。若想将文字居中显示,可以通过以下两种方法实现。 #### 方法一:使用自定义导航栏 使用自定义导航栏可以更加灵活地控制导航栏的样式,从而实现文字居中显示。具体实现步骤如下: 1. 在app.wxss文件定义自定义导航栏样式: ``` .custom-nav { height: 44px; line-height: 44px; text-align: center; font-size: 18px; color: #000; font-weight: bold; background-color: #fff; border-bottom: 1px solid #eee; } ``` 2. 在需要显示自定义导航栏的页面的wxml文件引入自定义导航栏: ``` <view class="custom-nav"> <text>{{title}}</text> </view> ``` 3. 在对应的js文件设置导航栏标题: ``` Page({ data: { title: '页面标题' }, onLoad: function() { wx.setNavigationBarTitle({ title: '' }) } }) ``` 通过调用`wx.setNavigationBarTitle`函数,将系统自带的导航栏标题设置为空,从而使用自定义导航栏。 #### 方法二:使用CSS样式控制 使用CSS样式控制也可以实现导航栏文字居中显示,具体实现步骤如下: 1. 在app.wxss文件设置导航栏样式: ``` .wx-navigation-bar .wx-navigation-bar__title { text-align: center; width: 100%; } ``` 2. 在需要显示导航栏标题的页面的js文件设置导航栏标题: ``` Page({ onLoad: function() { wx.setNavigationBarTitle({ title: '页面标题' }) } }) ``` 通过调用`wx.setNavigationBarTitle`函数,设置导航栏标题。 以上两种方法均可以实现微信小程序导航栏文字居中显示,开发者可根据实际需求选择相应的方法。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值