- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
简而言之,这就是我想要添加的自定义按钮,如下所示:
IMG 是我的 mipmap 文件夹中的一个 .png 文件,SOME TEXT 只是一个字符串值。添加的虚线只是作为图像中的分隔符,而不是按钮中的。问题是圆形边缘不会出现在添加图像的位置。它看起来像这样:
我的问题如下:
<solid />
<shape />
中的属性?我将不得不创建 10 个这样的按钮,每个按钮都有不同的颜色,如果我添加 android:color
不同的值,颜色不变custom_button.xml
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:topLeftRadius="250px"
android:bottomLeftRadius="250px"
android:topRightRadius="50px"
android:bottomRightRadius="50px" />
<solid
android:color="@color/YellowPrimary"/>
</shape>
button_styles.xml
<resources>
<style name="CategoryToggle">
<item name="android:background">@drawable/custom_button</item>
<item name="android:textAllCaps">true</item>
</style>
<style name="CategoryToggle.First">
<item name="android:color">@color/bluePrimary</item> // Does not override <solid>
<item name="android:drawableLeft">@mipmap/icon_48mdpi</item>
<item name="android:text">@string/first_cat</item>
</style>
</resources>
button_layout.xml
<android.support.constraint.ConstraintLayout
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">
<Button
style="@style/CategoryToggle.History"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
目前我没有 Java 代码,因为我刚开始尝试实现这种奇怪的按钮格式。
最佳答案
custom_button.xml
中定义的颜色应该引用颜色属性(类似 colorAccent
的东西应该适用于您的情况),而不是静态颜色。<<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="?attr/colorAccent"/>
</shape>
然后在您的按钮样式中更改此颜色,而不是 android:color
。
<style name="CategoryToggle">
<item name="colorAccent">@color/YellowPrimary</item>
</style>
确保您拥有 support library添加了依赖项,否则 colorAccent
将不可用。
使用 android:theme
属性,而不是 style
属性来应用按钮主题。
<Button
android:width="wrap_content"
android:height="wrap_content"
android:theme="@style/CategoryToggle"/>
icon_48mdpi.png
对于所有配置都应该被称为 icon_48dp.png
)和根据其密度放置在相应的可绘制文件夹中。您的可绘制资源应如下所示(在 Project
view structure 中,而不是 Android
View 结构)。res/
|-- drawable/
| +-- custom_button.xml
|-- drawable-hdpi/
| +-- icon_48dp.png
|-- drawable-mdpi/
| +-- icon_48dp.png
|-- drawable-xhdpi/
| +-- icon_48dp.png
|-- drawable-xxhdpi/
| +-- icon_48dp.png
|-- drawable-xxxhpdi/
| +-- icon_48dp.png
~
res/color/button_color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/pressed_color" android:state_pressed="true"/>
<item android:color="@color/focused_color" android:state_focused="true"/>
<item android:color="@color/state_hovered" android:state_hovered="true"/>
<item android:color="?attr/colorAccent"/>
</selector>
然后您可以在形状可绘制对象中使用此颜色资源,而不是 colorAccent
。
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/button_color_selector"/>
</shape>
您还可以通过 defining custom attributes 使颜色状态列表中的每种颜色成为可设置样式的属性并在您的样式中引用这些属性。不过,为了简洁起见,我不会进一步讨论。
您可以通过创建 state list drawable 来类似地为可绘制对象执行此操作.
最后,您需要养成使用 dp
而不是 px
的习惯,除非您绝对确定/strong> 你想使用 px
。这将防止在不同的屏幕密度下出现奇怪的外观。
关于android - 拥有android :drawableLeft with png时添加圆角,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55028388/
拥有/想要列表匹配算法 我正在一个高流量网站上实现一个元素交易系统。我有大量用户,每个用户都为许多特定项目维护一个 HAVE 列表和一个 WANT 列表。我正在寻找一种算法,使我能够根据您的 HAVE
我可以知道拥有 .hgtags 的目的是什么吗?我可以删除它吗?因为它不在 .hg文件夹,似乎“污染”了我的实际源代码目录。 https://bz.mercurial-scm.org/show_bug
在哪些用例中使用 [[nodiscard]] 有益类型? 关于类型,[[nodiscard]]如果任何返回该类型实例的函数的返回值被省略,则发出警告; (引自 p0068r0): If [[nodis
我知道当一个对象只有一个所有者时使用std::unique_ptr,当一个对象有多个所有者时使用std::shared_ptr。成为对象的唯一所有者意味着什么? 成为唯一所有者是否意味着其他人都无法看
我无法真正理解拥有 vendor 文件夹的目的。根据我了解到的情况, vendor 文件夹似乎只有在您尝试使您的存储库与早于 1.11 的 golang 版本兼容时才有用。 .我们正在运行 golan
我正在尝试学习如何在 Raspberry PI 上编码。我来自使用 Windows 和 VS Code 进行编码。现在我使用 Linux Mint 19.1 和 ssh 访问 Raspbian 4.1
我有一个带有“BlockType”枚举的图形程序。根据州的不同,这可能是以下几种情况之一:木头、石头、草地等。 最初,必须针对每种可能性执行各种纹理操作,但由于进行了一些重构,枚举仅用作整数,swit
我正在创建一个网络应用程序,让用户可以搜索餐馆和咖啡馆。由于我目前除了类型之外没有其他数据来区分这两者,所以我有两种存储餐馆列表的选择。 对餐厅和咖啡馆使用同一张表,并使用枚举(文本)列说明条目是餐厅
我有一个 5 人的小团队,我需要帮助寻找有关如何拥有一个集中式 MySQL 数据库的资源或建议。我们都以虚拟方式工作,并将所有文件托管在一个 github 上以集中所有内容。因此,在不同的计算机上拥有
SELECT dnum , SUM(IF(salary>20000, 1, 0)) AS Employee FROM project INNER JOIN works_on ON pno=pnumbe
我想在 javascript 中有一个数组,其中每个项目包含 2 个属性而不是 1 个,这怎么可能? 默认情况下,以下内容仅向项目添加一个属性: var headerCellWidths = new
我确定我对这个问题的措辞很糟糕,但这是我的问题: 我正在创建一长串地毯,每 block 地毯都有图片、标题、尺寸、原价和清仓价。我将每 block 地毯都放在自己的类别中,并对其余的地毯重复这样的操作
我正在编写一个延迟渲染器,并试图打包我的 gbuffer。 将漫反射和高光存储在一起会更好吗: vec4 difSpec = (diffuse.xyz, specular) // FORMAT_RGB
下面的程序给我一个链接时错误: #include struct Test { static constexpr char text[] = "Text"; }; int main() { s
想知道 - 如果了解Linux 的生活方式或 Linux 架构,是否会为在嵌入式设备上编程提供更好的思路,尤其是当它们具有某种操作系统时。 只是想确定我没有错过任何重要的事情:) 注意: 我来自 wi
如果要测试 iPhone 应用程序,拥有一个开发代码签名条款来运行任何带有 com.mydomain.* 的应用程序是不是一个坏主意? 为什么? 编辑: 请参阅上面的斜体编辑。 最佳答案 我对此表示怀
我正在开发我自己的 Swift 框架,以便在我的两个应用程序之间私下使用。我正在使用 Carthage 来管理该依赖项和其他依赖项。 我终于完成了框架的开发并将其连接到我的一个应用程序,毫不奇怪,该应
如果我有一个 C 类型的原始指针,是否可以从拥有指针数据的相同类型创建一个 std::vector 而无需任何数据复制(仅 move )?促使我问这个问题的是 std::vector 的 data()
有 2 个部分的好处是什么 - .data 和 .bss 用于流程范围变量。为什么不只拥有一个呢?我知道每个部分的用途。我正在使用 gcc。 最佳答案 .bss 消耗“内存”但不消耗可执行文件中的空间
我注意到,我们可以使用 StatefulWidget 构建 UI 的任何部分,而无需担心状态。 我想问的很简单,当我们可以将任何 UI 类构建为 StatefulWidget 时,首先拥有 State
我是一名优秀的程序员,十分优秀!