从头到尾创建线性渐变到android中的状态栏和工具栏

问题描述 投票:0回答:1

我想在状态栏和工具栏中添加线性渐变颜色。我可以向状态栏和工具栏添加渐变,角度为0度,显示从左到右的渐变颜色(pink on left and purple on right)like this但是我希望渐变流的角度为90度,从上到下或从下到上(eg: top pink and bottom purple)。我试过但我最终得到了这个和这个。这里状态栏和工具栏是分开的,我想要状态栏和工具栏具有相同的渐变颜色,具有90度this的渐变角度

android android-toolbar
1个回答
0
投票

在KitKat及更高版本中,您可以通过将状态栏设置为半透明并使用带有稀松布的全屏布局来控制状态栏和操作栏背景,同时注意将状态栏中的渐变混合到操作栏中。

需要注意的是,KitKat在我们自己的顶部使用自己的渐变稀松布,并且似乎无法覆盖,但在Lollipop +上没有这样的问题。

以下是MarshMallow模拟器上的工作活动示例:

Example output

这是我使用的代码:

RES /值/ styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="actionBarStyle">@style/MyActionBar</item>
</style>

<style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">@drawable/actionbar_bg</item>
</style>

<style name="AppTheme.ActionBar.Popup" parent="ThemeOverlay.AppCompat">
    <item name="android:colorBackground">@color/colorPrimaryDark</item>
    <item name="background">@color/colorPrimaryDark</item>
</style>

RES /值/ colors.xml:

<resources>
    <color name="colorPrimary">#ff7000e0</color>
    <color name="colorPrimaryDark">#ff400080</color>
    <color name="colorAccent">#ff40aba0</color>
</resources>

RES /绘制/ statusbar_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:angle="270"
        android:endColor="#ff7000e0"
        android:startColor="#ff8000ff" />
</shape>

RES /绘制/ actionbar_bg.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#ff7000e0"
        android:endColor="#ff400080"
        android:angle="270" />
</shape>

RES /布局/ activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:fitsSystemWindows="false" >

    <View
        android:id="@+id/main_activityStatusBarScrim"
        android:layout_width="match_parent"
        android:layout_height="26dp"
        android:background="@drawable/statusbar_bg"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/main_activityToolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@drawable/actionbar_bg"
        app:popupTheme="@style/AppTheme.ActionBar.Popup"/>

    <FrameLayout
        android:id="@+id/main_activityFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:background="#ffffffff">

    </FrameLayout>

</LinearLayout>

main activity.Java:

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // make the main layout fill the screen
        Window window = getWindow();
        final int layoutFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        window.getDecorView().setSystemUiVisibility(layoutFlags);
        supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);

        // make the status bar translucent
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(0x00000000);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // N.B. on some (most?) KitKat devices the status bar has a pre-defined gradient that
            // cannot be overridden.
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

        // inflate main layout and set up action bar
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.main_activityToolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }

        // set the height of the scrim to standard status bar height for this device (normally 26dp)
        View statusBarScrim = findViewById(R.id.main_activityStatusBarScrim);
        int height;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            height = getResources().getDimensionPixelSize(resourceId);
        } else {
            // belt and braces 
            height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 26f, getResources().getDisplayMetrics());
        }
        if (statusBarScrim != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                ViewGroup.LayoutParams lp = statusBarScrim.getLayoutParams();
                lp.height = height;
                statusBarScrim.setLayoutParams(lp);
                statusBarScrim.setVisibility(View.VISIBLE);
            }
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    ...
© www.soinside.com 2019 - 2024. All rights reserved.