android 以编程方式将自定义值设置为自定义文本视图
android set custom value to a custom textview programmatically
我有一个 TextView
的自定义 Class 。我已经将渐变属性实现为 textview 的颜色。
但我只在 xml
中实现了它。我是自定义视图的新手。我不知道如何在自定义 TextView class.
中添加 setStartColor
, setEndColor
values/attr
<declare-styleable name="GradientTextView">
<attr name="startColor" format="color" />
<attr name="endColor" format="color" />
</declare-styleable>
GradientTextView
public class GradientTextView extends AppCompatTextView {
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.GradientTextView);
int startColor = a.getColor(R.styleable.GradientTextView_startColor, Color.WHITE);
int endColor = a.getColor(R.styleable.GradientTextView_endColor, Color.WHITE);
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
a.recycle();
}
}
XML
<mehran.design.GradientTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:startColor="@color/yellow"
app:endColor="@color/blue"/>
定义变量 startColor
和 endColor
以及它的设置器
喜欢
public void setStartColor(int color) {
this.startColor= color;
--- do your logic----
invalidate();
}
引用Link
像这样:
public class GradientTextView extends AppCompatTextView {
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.GradientTextView);
int startColor = a.getColor(R.styleable.GradientTextView_startColor, Color.WHITE);
int endColor = a.getColor(R.styleable.GradientTextView_endColor, Color.WHITE);
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
a.recycle();
}
public void setCustomColor(int startColor,int endColor){
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
invalidate();
}
}
我有一个 TextView
的自定义 Class 。我已经将渐变属性实现为 textview 的颜色。
但我只在 xml
中实现了它。我是自定义视图的新手。我不知道如何在自定义 TextView class.
setStartColor
, setEndColor
values/attr
<declare-styleable name="GradientTextView">
<attr name="startColor" format="color" />
<attr name="endColor" format="color" />
</declare-styleable>
GradientTextView
public class GradientTextView extends AppCompatTextView {
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.GradientTextView);
int startColor = a.getColor(R.styleable.GradientTextView_startColor, Color.WHITE);
int endColor = a.getColor(R.styleable.GradientTextView_endColor, Color.WHITE);
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
a.recycle();
}
}
XML
<mehran.design.GradientTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:startColor="@color/yellow"
app:endColor="@color/blue"/>
定义变量 startColor
和 endColor
以及它的设置器
喜欢
public void setStartColor(int color) {
this.startColor= color;
--- do your logic----
invalidate();
}
引用Link
像这样:
public class GradientTextView extends AppCompatTextView {
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.GradientTextView);
int startColor = a.getColor(R.styleable.GradientTextView_startColor, Color.WHITE);
int endColor = a.getColor(R.styleable.GradientTextView_endColor, Color.WHITE);
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
a.recycle();
}
public void setCustomColor(int startColor,int endColor){
Shader myShader = new LinearGradient(0, 0, 0, 100,startColor, endColor, Shader.TileMode.CLAMP);
this.getPaint().setShader(myShader);
invalidate();
}
}