2016-06-29 6 views
0

Ich versuche, Klickeffekt auf Bildansicht in benutzerdefinierten Layout innerhalb der Symbolleiste platziert geben. So habe ich es wie folgt umgesetzt:Android Toolbar benutzerdefinierte Layout anklickbare Symbol mit Selektor animierten Hintergrund

diese in Symbolleiste enthalten ist:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:gravity="center_vertical" 
android:orientation="horizontal"> 

<LinearLayout 
     android:id="@+id/img_tool_back" 
     style="@style/SelectorLayout" 
     > 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_arrow_back_grey_600_24dp"/> 
    </LinearLayout> 
. 
. //Other action buttons 
. 
</Linearlayout> 

Selector Layout-Stil hat:

<style name="SelectorLayout"> 
    <item name="android:layout_width">?attr/actionBarSize</item> 
    <item name="android:layout_height">?attr/actionBarSize</item> 
    <item name="android:gravity">center</item> 
    <item name="android:background">@drawable/selector_action</item> 
</style> 

und animierte Aktion Selektor ist:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" 
android:exitFadeDuration="@android:integer/config_shortAnimTime"> 
<item android:drawable="@color/colorGray" android:state_selected="true"/> 
<item android:drawable="@color/colorGray" android:state_pressed="true"/> 
<item android:drawable="@color/colorGray" android:state_focused="true"/> 
<item android:drawable="@android:color/transparent"/> 
</selector> 

Ich habe mich gefragt, ob es möglich ist, denselben Effekt mit nur einer einzigen Bildansicht zu erzielen? anstatt Bildansicht im linearen Layout zu verwenden?

Antwort

1

Akut, können Sie verschiedene Drawables mit Ihrem Bild und unterschiedlicher Herkunft definieren, wie:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape> 
      <size 
       android:width="24dp" 
       android:height="24dp" /> 
      <solid android:color="@color/colorGrey" /> 
     </shape> 
    </item> 

    <item> 
     <bitmap android:src="@android:drawable/ic_arrow_back_grey_600_24dp" /> 
    </item> 
</layer-list> 

Dann ist dieses selector_action.xml im gelten. Schließlich verwenden Sie eine ImageButton mit android:background="@drawable/selector_action" anstelle von ImageView.

Ich hoffe, es hilft.