我正在尝试在具有背景颜色的线性布局上设置可选背景。我知道通常的方法是 android:background="?android:attr/selectableItemBackground"但我已经有另一个代码在后台。这是代码 fragment 。
<LinearLayout
android:id="@+id/number"
android:layout_width="150dp"
android:layout_height="130dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
android:background="@color/category_colors"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
问题是如何设置可选择的背景?
最佳答案
尝试剪下这段代码以了解大小:
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android foreground属性是比较新加入的XML属性,但是对API 22及以下版本不起作用!如果是这种情况,那么看起来我们将不得不在自定义 XML 文件中堆叠属性,但别担心,这比听起来容易!
1)在您的项目 View 中转到 res/drawable 文件夹
2)右击Drawable文件夹本身并选择新建>>>drawable资源文件
3)输入一个文件名my_custom_button.xml(根并不重要,因为你会用下面的代码替换它)
4) 如果您还没有,请单击 XML 文本选项卡(而不是设计 View )
5) 选择所有文本并基本上替换为以下内容: (创建自定义颜色边框基本上是相同的步骤)。 随意更改和使用颜色或将渐变(颜色混合)替换为您自己的自定义颜色!
<?xml version="1.0" encoding="utf-8"?>
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/category_colors">
<item android:id="@android:id/ripple">
<shape android:shape="rectangle">
<solid android:color="@color/colorPrimaryDark" />
<corners android:radius="@dimen/button_radius_large" />
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<gradient
android:angle="90"
android:endColor="@color/colorPrimaryLight"
android:startColor="@color/colorPrimary"
android:type="linear" />
<corners android:radius="@dimen/button_radius_large" />
</shape>
</item>
</ripple>
希望这对您有帮助!!!
关于java - 1 个 View android 布局上的 2 个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52169405/