Fragment中使用setSupportActionBar(),以及toolbar标题居中问题

问题1:Fragment中无法直接使用setSupportActionBar()
解决:
将得到的activity强制转化成AppCompatActivity

        val mActivity = activity as AppCompatActivity
        mActivity.setSupportActionBar(toolbar_fragment_more)
        var actionBar : ActionBar? = mActivity.supportActionBar

问题2:Toolbar的标题不居中
解决:将actionbar的标题设为空,然后在toolbar里放一个TextView控件
由于我直接放TextView不能使用layout_gravity这个属性,所有为了居中只能再放一个RalativeLayout,才能实现居中

在Activity中这样写

 if(actionBar != null){
            actionBar.title = ""
        }

在放Toolbar的布局中这样写


    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_fragment_more"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:background="@color/colorSkin">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginRight="18dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:text="更多"
                android:layout_centerInParent="true"/>
        </RelativeLayout>
    </androidx.appcompat.widget.Toolbar>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值