【发布时间】:2020-12-29 04:44:23
【问题描述】:
我正在尝试为我的 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>
这对应于我想要的颜色。但是,在可绘制资源文件中似乎不可能做到透明中心。
除了这个问题之外,在我的代码中,我在 LinearLayout 上方有一个 ListView,其中包含最终应应用此样式的三个按钮。但是,我什至无法更改按钮的背景颜色。
这是表示活动的代码:
<?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>
其他 StackOverflow 问题都没有解决我的任何一个问题。即使是最常见的“设置按钮的style 属性”之一。
我对可能需要外部工具或其他东西的解决方案持非常开放的态度,因此不要局限于内部功能。不过,我确实觉得 Android Studio 中必须有办法解决这个问题。
提前感谢任何花时间研究此问题的人!
这是一个应要求的示例(唯一的区别是我将使用可绘制/图像而不是文本)
【问题讨论】:
-
如果您不介意,您能否添加一个您想要的示例,图片示例会有所帮助
标签: android android-layout button gradient android-styles