Android 带有渐变轮廓和透明背景图标的自定义按钮

Android 带有渐变轮廓和透明背景图标的自定义按钮,android,android-layout,button,gradient,android-styles,Android,Android Layout,Button,Gradient,Android Styles,我正在尝试为我的android应用程序创建自定义按钮。这些按钮仅用于在屏幕上添加/编辑/删除数据。然而,我想做一些漂亮的按钮样式,但我有一些android编程的工作方式的困难 这个想法是有一个给定厚度(比如2dp)的边界,其颜色是渐变色。这意味着中心是透明的,但是我认为这是主要的困难之一。除此之外,我希望按钮的内部有一个代表其功能的图标 目前,我有渐变和它的圆形边缘,如下所示: <?xml version="1.0" encoding="utf-8"

我正在尝试为我的android应用程序创建自定义按钮。这些按钮仅用于在屏幕上添加/编辑/删除数据。然而,我想做一些漂亮的按钮样式,但我有一些android编程的工作方式的困难

这个想法是有一个给定厚度(比如2dp)的边界,其颜色是渐变色。这意味着中心是透明的,但是我认为这是主要的困难之一。除此之外,我希望按钮的内部有一个代表其功能的图标

目前,我有渐变和它的圆形边缘,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="top">
        <shape android:shape="rectangle">
            <gradient
                android:startColor="@color/colorPrimaryDark"
                android:centerColor="@color/colorAccent"
                android:endColor="@android:color/white"
                android:angle="270" />
            <corners android:radius="20dp"/>
        </shape>
    </item>
</layer-list>
其他问题都没有解决我的问题。即使是最常见的“设置按钮的
样式
属性”也是如此

我对可能需要外部工具或其他东西的解决方案非常开放,因此不觉得局限于内部功能。然而,我确实觉得在Android Studio中必须有办法解决这个问题

提前感谢所有花时间调查此事的人

这里是一个请求的示例(唯一的区别是我将使用可绘制/图像而不是文本)


我不知道这是否是最好的解决方案,但您可以通过以下方式轻松完成:

  • 创建
    gradient\u back.xml
  • 现在在布局中创建如下按钮:
  • res/values/styles
    中将父样式更改为:
  • 现在,您可以在布局中添加一个按钮:
  • 
    
    结果:


    我建议为此创建自定义视图。创建自定义视图将对您非常有益,正如前面提到的,您有许多按钮。使用自定义视图,可以使用不同的高度、宽度、边框宽度、颜色和角半径重复使用按钮

    阅读更多自定义视图。您还可以从一开始就通过一系列代码实验室

    attrs.xml(值/attrs.xml):

    
    

    如果您不介意,可以添加一个您想要的示例,一个图像示例会很有帮助。问题是,这在渐变背景上使用纯色填充,我需要一个透明的中心,这似乎非常困难。@MaxMichel啊,哦,对了,这改变了一切:/我将尝试考虑另一种解决方案。我将很快尝试。如果答案有效,我会接受(不尝试就接受是没有用的)。谢谢!
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".AddPlayersActivity"
        android:orientation="vertical"
        android:paddingRight="10dp"
        android:paddingLeft="10dp"
        android:background="@drawable/background_gradient">
    
        <TextView
            android:id="@+id/add_players_activity_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:fontFamily="@font/lato_bold"
            android:text="@string/app_name"
            android:textAlignment="center"
            android:textColor="@android:color/white"
            android:textSize="36sp" />
    
        <TextView
            android:layout_below="@+id/add_players_activity_title"
            android:id="@+id/add_players_activity_current_players"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lato"
            android:text="@string/current_players"
            android:textColor="@android:color/white"
            android:textSize="20sp" />
    
        <ListView
            android:id="@+id/add_players_activity_player_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/add_players_activity_current_players"
            android:layout_above="@id/add_players_activity_button_layout" />
    
        <LinearLayout
            android:id="@+id/add_players_activity_button_layout"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginHorizontal="10dp"
                android:text="Add"/>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginHorizontal="10dp"
                android:text="Edit"/>
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginHorizontal="10dp"
                android:text="Delete" />
        </LinearLayout>
    </RelativeLayout>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="10dp" />
        <gradient
            android:endColor="@color/colorPrimary"
            android:startColor="@color/colorAccent" />
    </shape>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <corners android:radius="9dp" />
        <solid android:color="@android:color/white" />
    </shape>
    
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient_back">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:background="@drawable/shape_front"
            android:text="WOW" />
    
    </FrameLayout>
    
    implementation "com.google.android.material:material:1.2.1"
    
    parent="Theme.MaterialComponents.Light.DarkActionBar"
    
    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="WOW"
        android:textColor="@color/textColor"
        app:cornerRadius="10dp"
        app:strokeWidth="2dp"
        app:strokeColor="@color/colorAccent" />