【问题标题】:Android : Image button or button Highlighted with Effect when PressedAndroid:按下时图像按钮或按钮突出显示效果
【发布时间】:2013-05-13 02:50:58
【问题描述】:

当我当时按下这些left and right arrow button 时,我想在按钮上看到这些类型的效果。默认情况下,Iphone/IOS 也会发生同样的事情。

我可以制作这种效果吗?

这里我提到了我想要的图片。

这里我使用了这个 xml 文件,但没有成功。

button_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true"><shape>
            <gradient android:angle="180" android:centerColor="#657377" android:endColor="#AFFFFFFF" android:startColor="#FFFFFFFF" android:type="linear"/>

            <corners android:radius="10dp" />
        </shape></item>

</selector>

编辑

我使用了android:background="@drawable/button_pressed.xml" 行,但我没有得到我想要的。

试过了:

我按照 Piyush 的回答使用了这个 xml 文件,但我没有成功,我得到了这个效果。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@color/transparent" />
        </shape>
    </item>
    <item>
        <shape
            android:innerRadiusRatio="4"
            android:shape="ring"
            android:thicknessRatio="9"
            android:useLevel="false" >
            <gradient
                android:endColor="#00000000"
                android:gradientRadius="250"
                android:startColor="#ffffffff"
                android:type="radial" />
        </shape>
    </item>

</layer-list>

我同意顶部和底部部分的削减,因为我在布局中为这个按钮设置了有限的空间。我们稍后会想到,但为什么它没有像我提到的阴影和 alpha 那样生效?

输出:

如果有人对此有任何想法,请帮助我。

提前致谢。

【问题讨论】:

  • 你有什么解决办法吗?即使我想达到同样的效果
  • @Goofy NO :( 你有吗?
  • 那你是怎么管理的,这种效果?我也在努力实现这种效果

标签: android android-layout animation android-animation android-gui


【解决方案1】:

形状必须是径向绘制的,向外从白色渐变为黑色。

试试这个:

<shape
    xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:startColor="#ffffffff"
        android:endColor="#00000000"
        android:gradientRadius="100"
        android:type="radial"/>
</shape>

此外,您不能直接将其设置为按钮的背景。您将必须创建一个选择器可绘制文件,您可以在其中根据按钮的状态指定不同的背景。在这种情况下,您需要仅在按下按钮时设置此背景。选择器将如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/button_pressed"/> 
    <item android:state_selected="true" android:drawable="@drawable/button_pressed"/>
    <item android:state_focussed="true" android:drawable="@drawable/button_pressed"/>
    <item android:drawable="@android:color/transparent" />
</selector>

PS:不建议在设计 Android 应用时复制 iOS 设计。 Android设计指南请参考:http://developer.android.com/design/building-blocks/buttons.html

【讨论】:

  • 感谢您的回复..我使用但我没有得到半径形状..我在矩形模式下生效..
  • 请确保您设置为源的可绘制箭头在箭头之外是透明的。还要确保按钮足够大以容纳此突出显示背景。另外,如果您可以发布使用我的解决方案后得到的效果,我将能够更好地评论它。
【解决方案2】:

对于圆形可以试试这个,可以相应地设置你的颜色。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape android:shape="rectangle" >
        <solid android:color="@color/transparent" />
    </shape>
</item>
<item>
    <shape

        android:shape="ring"
        android:useLevel="false" >
        <gradient
            android:endColor="#00000000"
            android:gradientRadius="200"
            android:startColor="#ffffffff"
            android:type="radial" />
    </shape>
</item>

【讨论】:

  • 感谢您的回答.. 所以我必须使用@ps2010 的答案,但只需用您的答案更改 shape xml 文件?
  • @Sam_k 是的,您可以检查状态新闻或仅关注形状。
  • 是的,我用过,但我得到的是椭圆形而不是半径形..我想我也必须设置 alpha..因为你可以在图像中看到从中心到外部的颜色逐渐变成白色到黑色
  • @Sam_k 你需要根据你的需要修改它,因为你可以看到我删除了不需要的东西,比如 android:innerRadiusRatio="4" 和 android:thicknessRatio="9"
  • 它也给出了相同的结果
猜你喜欢
  • 2017-01-25
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2020-08-22
  • 2011-03-05
  • 1970-01-01
  • 2011-07-04
  • 1970-01-01
相关资源
最近更新 更多