如何使用 Kotlin 以编程方式设置工具栏的边距



我希望状态栏充满操作栏的渐变背景。所以我搜索了一些文章并设法做到了。但我知道彩色状态栏需要 API 级别 21,所以我添加了条件来检查 API。在我的代码中,我有一个工具栏,它的上边距设置为 24dp,当 API 级别高于 21 时需要,所以如果 API 低于 21,我需要更改上边距。但是我不知道如何设置工具栏的边距

我尝试添加LayoutParams()但这不适用于工具栏。

我有一个单独的布局(action_bar.xml(用于操作栏,我将其包含在主要活动中

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
            android:id="@+id/appbarlayout"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:design="http://schemas.android.com/apk/res-auto"
            android:background="@drawable/gradient_theme"
            android:layout_width="match_parent"
            android:fitsSystemWindows="true"
            android:layout_height="80dp">
        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_marginTop="24dp"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                design:title="@string/app_name"
                design:titleTextColor="@android:color/white"/>
    </RelativeLayout> 

此外,我在设置内容视图后在我的 MainActivity.kotlin 中调用此函数:

fun beautifyLayout(activity:Activity,window:Window) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
        activity.findViewById<RelativeLayout>(R.id.bottom_nav).also {
            it.setPadding(0, 0, 0, getSoftButtonsBarSizePort(activity))
        }
    }
else{
//I want to remove the margin top if API level is less than 21
      val params = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.WRAP_CONTENT
        )
     params.topMargin = 0  
        val bar = activity.findViewById<Toolbar>(R.id.toolbar)
        bar.layoutParams = params
}
     }

这是我的主要活动:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:id="@+id/mainContainer">
    <include layout="@layout/action_bar"/>
    <include layout="@layout/bottom_nav"/>
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"
               android:paddingLeft="20dp" android:paddingRight="20dp" android:id="@+id/contentBox">
    <Button android:layout_width="match_parent" android:layout_height="wrap_content"
            android:text="@string/login"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:id="@+id/testButton"
            android:layout_marginTop="100dp"
    />
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

简而言之,如何在 Kotlin 中设置工具栏的边距?

您必须使用 ViewGroup.MarginLayoutParams 而不是 FrameLayout.LayoutParams

使用此代码

val bar = activity.findViewById<Toolbar>(R.id.toolbar)
val params = bar.layoutParams as ViewGroup.MarginLayoutParams
params.topMargin = 0
bar.layoutParams = params
您可以通过

创建目录res/layout-v21为 API 级别>= 21 创建替代布局,在该目录中创建一个名称为 action_bar.xml(与原始布局相同(的文件,其上边距和原始布局将没有边距

res/layout-v21/action_bar.xml

<RelativeLayout
        android:id="@+id/appbarlayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:design="http://schemas.android.com/apk/res-auto"
        android:background="@drawable/gradient_theme"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="80dp">
    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_marginTop="24dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            design:title="@string/app_name"
            design:titleTextColor="@android:color/white"/>
</RelativeLayout>

res/layout/action_bar.xml

<RelativeLayout
        android:id="@+id/appbarlayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:design="http://schemas.android.com/apk/res-auto"
        android:background="@drawable/gradient_theme"
        android:layout_width="match_parent"
        android:fitsSystemWindows="true"
        android:layout_height="80dp">
    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            design:title="@string/app_name"
            design:titleTextColor="@android:color/white"/>
</RelativeLayout> 

最新更新