首页 > 解决方案 > kotlin Android Studio 标签布局

问题描述

在此处输入图像描述

这里是activity_main.xml中的代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg2"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorTransparent"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabIndicatorColor="@android:color/transparent"
            app:tabIndicatorHeight="0dp"
            app:tabPaddingStart="0dp"
            app:tabPaddingEnd="0dp"
            app:tabGravity="fill"
            app:tabMode="fixed" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

这里是我在 Main_Activity.kt 文件中编写的代码:

//动作条

 val actionBar = supportActionBar
actionBar!!.setDisplayShowHomeEnabled(true)

actionBar.setBackgroundDrawable(ColorDrawable(Color.parseColor("#00FFFFFF")))
actionBar.setIcon(R.drawable.title)
actionBar.setDisplayShowTitleEnabled(false)

标签: androidtabskotlin

解决方案


  1. 您可以为操作栏创建自定义布局,让您控制图标的外观。但是,由于这样做会遇到数百万个问题,我是否可以建议您从 usingActionBar转移到 API 21 中引入的它的继任者 ,Toolbar您应该使用它:

https://guides.codepath.com/android/Using-the-App-ToolBar

借助设计库,您可以轻松地在 中引入完全可定制的工具栏CoordinatorLayout,并做一些很酷的事情。

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentInsetStart="0dp">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/qpon_drawable"
                android:layout_margin="20dp"
                android:layout_gravity="center"/>
        </FrameLayout>
    </android.support.v7.widget.Toolbar>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabIndicatorColor="@android:color/transparent"
        app:tabIndicatorHeight="0dp"
        app:tabPaddingStart="0dp"
        app:tabPaddingEnd="0dp"
        app:tabGravity="fill"
        app:tabMode="fixed" />

</android.support.design.widget.AppBarLayout>

在此处输入图像描述

我可以添加我想要的任何边距,并且app:contentInsetStart="0dp"没有开始填充。您可以将工具栏添加为代码中的支持操作栏:

    // Find the toolbar view inside the activity layout
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    // Sets the Toolbar to act as the ActionBar for this Activity window.
    // Make sure the toolbar exists in the activity and is not null
    setSupportActionBar(toolbar);
  1. 当我将背景颜色设置AppBarLayout为透明时,我遇到了同样的边框问题:

在此处输入图像描述

当我将背景设置为与AppBarLayout's背景相同的颜色时,它似乎在边缘绘制并且边框消失了。请记住,它会自动在底部给它一个阴影,因为它高于Viewpager:

 <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    <!-- This is what you want to change !-->
    android:background="@android:color/background_light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

在此处输入图像描述

侧面不再有边框。我不太清楚它为什么会这样,如果你不上色的话,它似乎是一种自然形式的卡片布局。

希望能帮助到你。无论您的推理如何,请尝试更改为工具栏,我认为这是唯一正确的答案!


推荐阅读