2016-01-23 94 views
1

有一种叫做Widget.Material.Button的风格,它具有我非常喜欢的动画效果(它会让点击产生“飞溅”),我希望能够在代码中重用它。我该如何模仿棒棒糖(Material Design)按钮动画?

我试图让它具有透明背景色,因为它默认为灰色,在应用于我的ViewGroup对象时看起来不太好。

有没有简单的方法来保持动画,但摆脱背景颜色?

我已经尝试设置属性android:background="@android:color/transparent",但这会导致动画完全停止工作。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    style="@android:style/Widget.Material.Button" 
    tools:targetApi="lollipop"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello, world!"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="This is just another line of text."/> 

</LinearLayout> 

回答

3

正如this blog下 '可点击查看' 中提到,您可以使用android:background="?attr/selectableItemBackground

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="?attr/selectableItemBackground"> 
    ... 
</LinearLayout> 
+0

完美,所提供的链接也很丰富。 – zxgear

2

对不起,最初给出了错误的位。问题看起来更深一点的是,背景颜色已被定义为您为MaterialTheme扩展程序使用的任何颜色高亮部分的一部分。具有讽刺意味的是,他们有来自其他地方的透明来源,但不在这里

你想那么,两个新的XML文件来定义你的属性

在瓦莱斯-V21/styles.xml你可以重新定义这些你喜欢的任何方式或消除值你不希望覆盖

什么

如果你只是想纹波,使用已定义的在底部

<style name="MyMaterialButton" parent=android:Widget.Material.Button> 
     <item name="background">@drawable/material_background_ripple</item> 
     <item name="textAppearance">?attr/textAppearanceButton</item> 
     <item name="minHeight">48dip</item> 
     <item name="minWidth">88dip</item> 
     <item name="stateListAnimator">@anim/button_state_list_anim_material</item> 
     <item name="focusable">true</item> 
     <item name="clickable">true</item> 
     <item name="gravity">center_vertical|center_horizontal</item> 
    </style> 

然后你的按钮背景样式提拉CODE /drawable-v21/material_background_ripple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android" 
     android:color="@android:color/PICK_RIPPLE_COLOR_HERE"> 
    <item android:drawable="@drawable/btn_default_mtrl_shape" /> 
</ripple> 

节录: 你试过 customView.getWindow()setBackgroundDrawableResource(R.color.transparent)。

+0

没有名为'getWindow()''为对象View'这样的方法。我已经尝试以编程方式设置背景颜色,它不起作用。设置“活动”窗口的背景颜色也无济于事。 – zxgear

+0

请上面阅读。我想我最初误解了你的问题。 – childofthehorn