如何制作一个 android 凸起的按钮?
How to make an android raised button?
我已经在网上搜索了一周左右的时间,想找到有关如何制作彩色凸起按钮的示例或教程,但一无所获。
我希望在我的应用程序中实现以下内容以获得更好的用户界面体验。
我确实遇到过卡片视图,但是当你编写一个包含很多按钮的大程序时,xml 代码会因为这个卡片视图而变大。
因此,如果对以下问题有任何快速简单的解决方案,请告诉我。
谢谢
已更新
Normal button
and with color
您可以试试这个替代方案:
在 drawable 文件夹中创建一个 xml
:
cardlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient android:angle="90"
android:endColor="@color/background_gray" android:startColor="#ccc" />
<corners android:radius="4dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="1.5dp"
android:top="0dp"
android:bottom="1.5dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
现在要提升任何 view
(按钮、ListRow 或图像视图)。只需使用此卡片布局设置该视图的背景。
android:background="@drawable/cardlayout"
注意根据需要更改卡片布局背景颜色。
我认为您实际上想要提升按钮。不要使用卡片,因为它需要更多资源。对于棒棒糖设备使用
<Button
...
android:stateListAnimator="@anim/my_animator" />
并在资源的 anim 文件夹中创建 my_animator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="@dimen/button_pressed_z_material"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType"/>
</set>
</item>
<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="0"
android:startDelay="@integer/button_pressed_animation_delay"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>
...
</selector>
看看这个:-
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="60dp"
android:text="Raised Color Button"
android:textSize="15sp"
android:backgroundTint="#1E9D96"
android:textColor="@android:color/background_light" />
您可以使用:
android:elevation="2dp"
但它只适用于 Lollipop 及以上版本。我建议在 Android.
的较低版本的情况下使用图层列表
我已经在网上搜索了一周左右的时间,想找到有关如何制作彩色凸起按钮的示例或教程,但一无所获。
我希望在我的应用程序中实现以下内容以获得更好的用户界面体验。
我确实遇到过卡片视图,但是当你编写一个包含很多按钮的大程序时,xml 代码会因为这个卡片视图而变大。
因此,如果对以下问题有任何快速简单的解决方案,请告诉我。
谢谢
已更新
Normal button
and with color
您可以试试这个替代方案:
在 drawable 文件夹中创建一个 xml
:
cardlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient android:angle="90"
android:endColor="@color/background_gray" android:startColor="#ccc" />
<corners android:radius="4dp" />
</shape>
</item>
<item
android:left="0dp"
android:right="1.5dp"
android:top="0dp"
android:bottom="1.5dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="4dp" />
</shape>
</item>
</layer-list>
现在要提升任何 view
(按钮、ListRow 或图像视图)。只需使用此卡片布局设置该视图的背景。
android:background="@drawable/cardlayout"
注意根据需要更改卡片布局背景颜色。
我认为您实际上想要提升按钮。不要使用卡片,因为它需要更多资源。对于棒棒糖设备使用
<Button
...
android:stateListAnimator="@anim/my_animator" />
并在资源的 anim 文件夹中创建 my_animator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="@dimen/button_pressed_z_material"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType"/>
</set>
</item>
<!-- base state -->
<item android:state_enabled="true">
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="@integer/button_pressed_animation_duration"
android:valueTo="0"
android:startDelay="@integer/button_pressed_animation_delay"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="@dimen/button_elevation_material"
android:valueType="floatType" />
</set>
</item>
...
</selector>
看看这个:-
<Button
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="60dp"
android:text="Raised Color Button"
android:textSize="15sp"
android:backgroundTint="#1E9D96"
android:textColor="@android:color/background_light" />
您可以使用:
android:elevation="2dp"
但它只适用于 Lollipop 及以上版本。我建议在 Android.
的较低版本的情况下使用图层列表