如何在xamarin android中将状态栏和工具栏与渐变结合起来

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

I want to combine status bar and toolbar with gradient like this

我试图将状态栏设置为透明,并使工具栏变大并设置渐变。但它也使您的UI在软导航键下。

软键盘也覆盖了EditTexts。 android:windowSoftInputMode =“adjustPan”无法正常工作

if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
     Window window = Window;
     window.AddFlags(WindowManagerFlags.LayoutNoLimits);
     window.AddFlags(WindowManagerFlags.TranslucentNavigation);
}

有没有办法只有状态栏透明而不是透明底部的状态栏和导航按钮。

xamarin.android
3个回答
2
投票

您也可以这样做,将代码添加到OnCreate方法中:

protected override void OnCreate(Bundle savedInstanceState)
  {

  if (Build.VERSION.SdkInt >= Build.VERSION_CODES.Kitkat)
    {
      Window w = Window;
      w.AddFlags(WindowManagerFlags.TranslucentStatus);
      w.SetSoftInputMode(SoftInput.StateHidden|SoftInput.AdjustResize);
    }
      base.OnCreate(savedInstanceState);
      SetContentView(Resource.Layout.activity_other);
 }

然后在Resources / drawable中创建gradient.xml:

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

最后在layout.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"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:orientation = "vertical"
              android:layout_height="match_parent"
              android:fitsSystemWindows= "true"
              android:background= "@drawable/gradient"
           >

   <!--use Toolbar-->
   <android.support.v7.widget.Toolbar
      android:id="@+id/toolbar"
      android:layout_width="match_parent"
      android:layout_height="?attr/actionBarSize"
      app:title="标题"
      app:titleTextColor="#fff"
      app:subtitle="副标题"
      app:subtitleTextColor="#fff"/>

   <!--if dont use Toolbar,RePresent Toolbar-->
   <LinearLayout
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="44dp">

      <ImageView
          android:layout_gravity="center_vertical"
          android:src="@drawable/ic_arrow_back_white_24dp"
          android:layout_marginLeft="10dp"
          android:layout_width="35dp"
          android:layout_height="35dp" />

      <TextView
          android:layout_gravity="center_vertical"
          android:textSize="25sp"
          android:gravity="center_horizontal"
          android:textColor="#fff"
          android:text="Test Title"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" />
  </LinearLayout>
  <ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fillViewport="true"
          android:id="@+id/scrollView1" 
          android:background = "#fff" //set the background color of your content
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation ="vertical" >
         <!--your content -->
           ...
     </LinearLayout>
  </ScrollView>
</LinearLayout>

0
投票

为什么不将状态栏的颜色设置为与渐变的startColor相同,并让工具栏从它的位置开始。


0
投票

创建一个静态方法,设置状态栏颜色如下所示:

 public static void SetStatusBarGradiant(Activity activity)
    {
        if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
        {
            Window window = activity.Window;
            Drawable background = ResourcesCompat.GetDrawable(activity.Resources, Resource.Drawable.abc_ab_share_pack_mtrl_alpha, null);
            window.AddFlags(WindowManagerFlags.DrawsSystemBarBackgrounds);
            window.SetStatusBarColor(Android.Graphics.Color.Transparent);
            window.SetNavigationBarColor(Android.Graphics.Color.Transparent);
            window.SetBackgroundDrawable(background);
        }
    }

然后在可绘制的东西中添加一个gradient.xml,如下所示,根据您的要求设置起始颜色和结束颜色:

gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
   <shape>
    <gradient
        android:angle="90"
        android:startColor="#FFFF0000"
        android:endColor="#FF00FF00"
        android:type="linear" />
   </shape>
</item>
</selector>

祝好运

如果您有疑问,请恢复

© www.soinside.com 2019 - 2024. All rights reserved.