2014-11-03 21 views
19

Yeni uygulama tasarımından yeni malzeme tasarımı actionbar'ı ekledim ve yeni araç çubuğu widget'ını kullanıyorum. Xml'de araç çubuğunda özel bir arka plan belirledim ama benim sorunum, actionbardan gelen gölge görünmüyor olmasıdır. Bunun nasıl yapılacağını biliyor musun?Android: Appcompat 21 actionbar'da gölge ekleme nasıl

Çubuğu kodu

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/my_awesome_toolbar" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="@drawable/ab_background_textured" 
    app:theme="@style/MyTheme" 
    app:popupTheme="@style/MyTheme.Popup"/> 

Mytheme tarzı

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item> 
    <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item> 
    <item name="android:textColorSecondary">#ffff8800</item> 
</style> 

MyTheme.Popup tarzı

<style name="MyTheme.Popup" parent="ThemeOverlay.AppCompat.Dark"> 
    <item name="android:textColor">#ffffff</item> 
</style> 

Güncelleştirme

Beğen @Justin Powell, temama actionBarStyle eklediğimi ancak yine de gölge olmadığını belirtiyor.

Mytheme stili (Güncellendi) öncesi 5.0 düzenleri için

<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:textColorPrimary">@color/abc_primary_text_material_dark</item> 
    <item name="actionMenuTextColor">@color/abc_primary_text_material_dark</item> 
    <item name="android:textColorSecondary">#ffff8800</item> 
    <item name="android:actionBarStyle">@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse</item> 
</style> 
+0

Olası kopyalar [destek kütüphanesinin İşlem Çubuğu & Araç Çubuğu gölgesini nasıl gösterilir tüm Android sürümleri?] (http://stackoverflow.com/questions/27474766/how-to-sho w-shadow-of-the-actionbartoolbar-of-the-eylem-kütüphane-on-all-an) – rds

cevap

30

, sen içeriğin FrameLayout için foreground="?android:windowContentOverlay" ekleyerek geri araç çubuğunun altındaki içeriğe gölge ekleyebilir. Örneğin

:

<!-- API level 21 and above then the elevation attribute is enough. For some reason it can't be set directly on the include so I wrap it in a FrameLayout --> 
<FrameLayout 
    android:id="@+id/topwrapper" 
    android:background="@color/theme_primary" 
    android:elevation="4dp" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <include layout="@layout/toolbar_actionbar" /> 
</FrameLayout> 

<FrameLayout 
    android:layout_below="@id/topwrapper" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <WebView 
     android:id="@+id/webview" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
    <!-- This topshadow is hidden in code for API level 21 and above --> 
    <include layout="@layout/topshadow" /> 
</FrameLayout> 

Sonra topshadow düzeni (istediğiniz gölge yüksekliği elde etmek 5DP ayarlayın) şöyle görünür:

<?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.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/actionBarSize" 
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" /> 

<FrameLayout 
    android:id="@+id/fragmentContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:foreground="?android:windowContentOverlay" 
    /> 

</LinearLayout> 
+0

Google, bu özniteliği "Google IO 2014" uygulamalarına ayarlamadan gölgeyi nasıl elde ettiğinizi biliyor muydunuz? (https://github.com/google/iosched/blob/master/android/src/main/res/layout/activity_settings.xml) –

+5

Bu, başka bir etkinlik düzeninde kullanılabilecekleri çizilebilir: https: // github. com/google/iosched/blob/master/android/src/main/res/drawable-xxhdpi/bottom_shadow.9.png ön plan olarak. –

+0

@Billy Maalesef android: windowContentOverlay daha sonra orijinal gölge daha hafiftir, aynı görünmüyor. Orijinal görünüşü taklit etmek için hangi sistem kaynağı kullanılabilir? – tomrozb

12

Bu gölgeyi görüntülemek nasıl:

<View xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="5dp" 
    android:id="@+id/shadow_prelollipop" 
    android:background="@drawable/background_shadow" /> 

background_shadow.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 

    <gradient 
     android:startColor="#02444444" 
     android:endColor="#33111111" 
     android:angle="90"></gradient> 
</shape> 

toolbar_actionbar.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:myapp="http://schemas.android.com/apk/res-auto" 
    myapp:theme="@style/ActionBarThemeOverlay" 
    myapp:popupTheme="@style/ActionBarPopupThemeOverlay" 
    android:id="@+id/toolbar_actionbar" 
    android:background="@color/theme_primary" 
    myapp:titleTextAppearance="@style/ActionBar.TitleText" 
    myapp:contentInsetStart="?actionBarInsetStart" 
    android:layout_width="match_parent" 
    android:layout_height="?actionBarSize" /> 
33

Google IO uygulamasında çözüm kendim için kabul edilebilir olması için bulundu, ancak hiçbir blog veya Stackoverflow sonrası tam olarak açıklayan gördü. Ne yapabilirsiniz sizin Faaliyet düzeninde sonra https://github.com/google/iosched/blob/master/android/src/main/res/drawable-xxhdpi/bottom_shadow.9.png kendi Apache 2 lisanslı çekmece gölge varlık kapmak ve geçerli: başlık gölge Oysa

<RelativeLayout android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <include android:id="@+id/toolbar" 
      layout="@layout/toolbar"/> 

    <FrameLayout android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@id/toolbar" 
       android:foreground="@drawable/header_shadow"> 
    <!-- YOUR STUFF HERE --> 
    </FrameLayout> 
</RelativeLayout> 

API düzeyleri < 21. Tıpkı için

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <item name="header_shadow" type="drawable">@drawable/bottom_shadow</item> 
</resources> 

olduğunu https://github.com/google/iosched/blob/master/android/src/main/res/values/refs.xml ve https://github.com/google/iosched/blob/master/android/src/main/res/values-v21/refs.xml.

Ve ayrıntılı olarak, burada toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="?attr/colorPrimary" 
     android:elevation="4dp" 
     app:theme="@style/ToolbarTheme" 
     app:popupTheme="@style/AppTheme"/> 
+4

Oh Bu harika derecede korkunç, ama mantıklı bir yaklaşım gibi görünüyor. Bunu deneyeceğim. Teşekkürler! – benhylau

+0

> = Lollipop üzerinde iki gölgeniz olmaz mı? – southerton

+0

Hayır, 2 gölgeyle sona ermiyorsunuz çünkü 21 ve üstü için header_shadow boş. Refsxml değerlerinde v21 bakın. –

0

actionbar_background olduğunu.xml

<item> 
     <shape> 
      <solid android:color="@color/black" /> 
      <corners android:radius="2dp" /> 
      <gradient 
       android:startColor="@color/black" 
       android:centerColor="@color/black" 
       android:endColor="@color/white" 
       android:angle="270" /> 
     </shape> 
    </item> 

    <item android:bottom="3dp" > 
     <shape> 

      <solid android:color="#ffffff" /> 
      <corners android:radius="1dp" /> 
     </shape> 
    </item> 
</layer-list> 

eklenti Basetheme eklemek geçmiş oluşturdu

<style name="Theme.ActionBar" parent="style/Widget.AppCompat.Light.ActionBar.Solid"> 
    <item name="background">@drawable/actionbar_background</item> 
    <item name="android:elevation">0dp</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:layout_marginBottom">5dp</item> 
    <item name="logo">@drawable/ab_logo</item> 
    <item name="displayOptions">useLogo|showHome|showTitle|showCustom</item> 
</style> 

actionbar_style için

<style name="BaseTheme" parent="Theme.AppCompat.Light"> 
      <item name="android:homeAsUpIndicator">@drawable/home_back</item> 
      <item name="actionBarStyle">@style/TFKBTheme.ActionBar</item> 
    </style> 
ait