用户输入后在Java for Android中更改形状的颜色
我有一个简单的形状,它存储在rect.xml中,并作为“视图”的“背景”属性包含在另一个xml中。这两个代码是: \res\layout\rect.xml:用户输入后在Java for Android中更改形状的颜色,java,android,xml,colors,shape,Java,Android,Xml,Colors,Shape,我有一个简单的形状,它存储在rect.xml中,并作为“视图”的“背景”属性包含在另一个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="#
<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>
我的另一个视图显示为活动的内容,包括:
<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总是相同的。
提前感谢您的回答 我还没有测试过这个,但是基于这些信息 您应该能够使用以下选项设置颜色:
GradientDrawable gradient=(GradientDrawable)yourView.getBackground();
int startColor=Color.BLACK;
int endColor=Color.RGB(0,2255);
gradient.setColors(新int[]{startColor,endColor});
请注意,GradientDrawable.setColors()是在API level 16/Android 4.1中添加的(谢谢!),为了支持早期版本,您需要在每次更改颜色时创建并设置一个新的GradientDrawable
,如下所示:
GradientDrawable gradient=新的GradientDrawable(Orientation.TOP\u BOTTOM,new int[]{startColor,endColor});
gradient.setShape(GradientDrawable.长方形);
yourView.backbackgrounddraw(坡度);
我没有测试过这个,但基于这些信息
您应该能够使用以下选项设置颜色:
GradientDrawable gradient=(GradientDrawable)yourView.getBackground();
int startColor=Color.BLACK;
int endColor=Color.RGB(0,2255);
gradient.setColors(新int[]{startColor,endColor});
请注意,GradientDrawable.setColors()是在API level 16/Android 4.1中添加的(谢谢!),为了支持早期版本,您需要在每次更改颜色时创建并设置一个新的GradientDrawable
,如下所示:
GradientDrawable gradient=新的GradientDrawable(Orientation.TOP\u BOTTOM,new int[]{startColor,endColor});
gradient.setShape(GradientDrawable.长方形);
yourView.backbackgrounddraw(坡度);
我没有测试过这个,但基于这些信息
您应该能够使用以下选项设置颜色:
GradientDrawable gradient=(GradientDrawable)yourView.getBackground();
int startColor=Color.BLACK;
int endColor=Color.RGB(0,2255);
gradient.setColors(新int[]{startColor,endColor});
请注意,GradientDrawable.setColors()是在API level 16/Android 4.1中添加的(谢谢!),为了支持早期版本,您需要在每次更改颜色时创建并设置一个新的GradientDrawable
,如下所示:
GradientDrawable gradient=新的GradientDrawable(Orientation.TOP\u BOTTOM,new int[]{startColor,endColor});
gradient.setShape(GradientDrawable.长方形);
yourView.backbackgrounddraw(坡度);
我没有测试过这个,但基于这些信息
您应该能够使用以下选项设置颜色:
GradientDrawable gradient=(GradientDrawable)yourView.getBackground();
int startColor=Color.BLACK;
int endColor=Color.RGB(0,2255);
gradient.setColors(新int[]{startColor,endColor});
请注意,GradientDrawable.setColors()是在API level 16/Android 4.1中添加的(谢谢!),为了支持早期版本,您需要在每次更改颜色时创建并设置一个新的GradientDrawable
,如下所示:
GradientDrawable gradient=新的GradientDrawable(Orientation.TOP\u BOTTOM,new int[]{startColor,endColor});
gradient.setShape(GradientDrawable.长方形);
yourView.backbackgrounddraw(坡度);
谢谢你的帮助!这确实有效(经过测试);但是,GradientDrawable.setColor()
方法被添加到JellyBean(API 16)中的平台中。如果需要支持ICS(或以前的),那么就有更多的环可以跳过。。。可能是将颜色存储为资源,然后在运行时构建背景。谢谢@unrulygnu,我没有意识到这一点。包含在原始答案中。感谢您的帮助!这确实有效(经过测试);但是,GradientDrawable.setColor()
方法被添加到JellyBean(API 16)中的平台中。如果需要支持ICS(或以前的),那么就有更多的环可以跳过。。。可能是将颜色存储为资源,然后在运行时构建背景。谢谢@unrulygnu,我没有意识到这一点。包含在原始答案中。感谢您的帮助!这确实有效(经过测试);但是,GradientDrawable.setColor()
方法被添加到JellyBean(API 16)中的平台中。如果需要支持ICS(或以前的),那么就有更多的环可以跳过。。。可能是将颜色存储为资源,然后在运行时构建背景。谢谢@unrulygnu,我没有意识到这一点。包含在原始答案中。感谢您的帮助!这确实有效(经过测试);但是,GradientDrawable.setColor()
方法被添加到JellyBean(API 16)中的平台中。如果需要支持ICS(或以前的),那么就有更多的环可以跳过。。。可能是将颜色存储为资源,然后在运行时构建背景。谢谢@unrulygnu,我没有意识到这一点。包括在原始答案中。