2016-04-28 44 views
6

任何人都可以帮我解决以下问题。因为我必须将渐变应用到状态栏。我知道只通过他们colorPimaryDark做状态栏的单一颜色。 enter image description here如何在android中将渐变应用于状态栏?

正如您在图像中看到的那样,它在状态栏中显示与验证布局中相同的渐变。

感谢

+1

[http://meta.stackoverflow.com/questions/266384/is-it-ok-to-downvote-questions-asking-about-how-to-achieve-something-without-ha ](http://meta.stackoverflow.com/questions/266384/is-it-ok-to-downvote-questions-asking-about-how-to-achieve-something-without-ha) –

+1

设置状态栏透明和使工具栏变大并设置渐变。 –

+0

主题? @ZahidulIslam –

回答

18

你的OnCreate应该是这样的

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
     Window w = getWindow(); 
     w.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 
     w.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 
    } 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

布局文件应该是这样的。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" 
    android:background="#fff" 
    tools:context="sslwireless.com.testfullscreen.MainActivity"> 

    <!--RePresent Toolbar--> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:background="@drawable/gradient" 
     android:layout_width="match_parent" 
     android:layout_height="100dp"> 

     <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> 


</RelativeLayout> 

而渐变文件看起来像这样。

<?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> 

用户界面将看起来像这样。

enter image description here

+0

谢谢你的好答案和奉献。有没有办法只有状态栏透明,而不是透明的底部状态栏和导航按钮。 –

+0

android:windowSoftInputMode =“adjustPan”不工作:(现在软键盘覆盖了EditTexts。 – Khan

相关问题