Android-ActionBar以编程方式设置高程

时间:2018-07-19 08:09:00

标签: android android-actionbar android-toolbar

styles.xml

 style name="AppTheme.AppBarOverlay"
 parent="ThemeOverlay.AppCompat.Dark.ActionBar"

 style name="AppTheme.PopupOverlay"
 parent="ThemeOverlay.AppCompat.Light"

fragment_container.xml

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/appBarLayout"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

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

我在onCreate回调中使用以下代码添加工具栏

var pageLayoutContainer = activity.getLayoutInflater()。inflate(NativeSFR.layout.page_container_layout,null);

fragmentLayoutContainer.findViewById(R.id.fragmentRootLayout);
toolbar = fragmentLayoutContainer.findViewById(R.id.toolbar);
activity.setSupportActionBar(toolbar);
actionBar = activity.getSupportActionBar();

我要删除工具栏下的阴影。但是下面的代码对我不起作用。我的设备api级别大于21。

actionBar.setElevation(0);

3 个答案:

答案 0 :(得分:1)

尝试一下:

AppBarLayout appBarLayout = findViewById(R.id.appBarLayout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    appBarLayout.setElevation(getResources().getDimension(R.dimen.elevation));
}

答案 1 :(得分:0)

尝试一下:-

ViewCompat.setElevation(View, int);

getSupportActionBar().setElevation(0);

答案 2 :(得分:0)

我删除了AppBarLayout。我在运行时将工具栏的高程设置为0。这个对我有用。

getSupportActionBar().setElevation(0);

但是我不明白为什么海拔0不能正常工作(使用AppBarLayout)。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorYellow"
            android:elevation="4dp"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar"
        android:background="@color/colorYellow"
        android:id="@+id/page_layout"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </FrameLayout>

</RelativeLayout>