在用户输入后为 Android 更改 Java 中形状的颜色
Change colour of a shape in Java for Android after user input
我有一个简单的形状存储在 rect.xml 中,并作为 'View' 的 'background' 属性包含在另一个 xml 中。这两个代码是:
\res\layout\rect.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shape"
android:shape="rectangle" >
<stroke android:width="2dp" android:color="#aaaaff" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<solid android:color="#E6121A" />
<gradient
android:startColor="#000000"
android:endColor="#aaaaff"
android:angle="90" >
</gradient>
<corners
android:radius="10dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" >
</corners>
</shape>
我的另一个视图,显示为 activity 的内容包括:
<View
android:id="@+id/myRectangleView"
android:layout_width="25dp"
android:layout_height="125dp"
android:layout_below="@+id/tableLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:background="@layout/rect" />
我想将 android:endColor 属性设置为另一种颜色,startColor 始终相同。
提前感谢您的回答!
我没有测试过这个,但是根据信息here
你应该能够用这个设置颜色:
GradientDrawable gradient = (GradientDrawable)yourView.getBackground();
int startColor = Color.BLACK;
int endColor = Color.RGB(0, 2, 255);
gradient.setColors(new int[]{ startColor, endColor });
请注意 GradientDrawable.setColors() 已添加到 API 级别 16/Android 4.1(感谢 unrulygnu!),并且要支持您需要的早期版本每次要更改颜色时创建并设置一个新的 GradientDrawable
,如下所示:
GradientDrawable gradient = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{startColor, endColor});
gradient.setShape(GradientDrawable.RECTANGLE);
yourView.setBackgroundDrawable(gradient);
我有一个简单的形状存储在 rect.xml 中,并作为 'View' 的 'background' 属性包含在另一个 xml 中。这两个代码是:
\res\layout\rect.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shape"
android:shape="rectangle" >
<stroke android:width="2dp" android:color="#aaaaff" />
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" >
</padding>
<solid android:color="#E6121A" />
<gradient
android:startColor="#000000"
android:endColor="#aaaaff"
android:angle="90" >
</gradient>
<corners
android:radius="10dp"
android:topLeftRadius="0dp"
android:topRightRadius="0dp" >
</corners>
</shape>
我的另一个视图,显示为 activity 的内容包括:
<View
android:id="@+id/myRectangleView"
android:layout_width="25dp"
android:layout_height="125dp"
android:layout_below="@+id/tableLayout1"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:background="@layout/rect" />
我想将 android:endColor 属性设置为另一种颜色,startColor 始终相同。 提前感谢您的回答!
我没有测试过这个,但是根据信息here 你应该能够用这个设置颜色:
GradientDrawable gradient = (GradientDrawable)yourView.getBackground();
int startColor = Color.BLACK;
int endColor = Color.RGB(0, 2, 255);
gradient.setColors(new int[]{ startColor, endColor });
请注意 GradientDrawable.setColors() 已添加到 API 级别 16/Android 4.1(感谢 unrulygnu!),并且要支持您需要的早期版本每次要更改颜色时创建并设置一个新的 GradientDrawable
,如下所示:
GradientDrawable gradient = new GradientDrawable(Orientation.TOP_BOTTOM, new int[]{startColor, endColor});
gradient.setShape(GradientDrawable.RECTANGLE);
yourView.setBackgroundDrawable(gradient);