2016-04-28 40 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

相關問題