java - 如何更改之前创建的自定义视图的一个属性?
问题描述
我有一个创建自定义视图的类。我称它们为植物视图。他们每个人都有这些属性:姓名、学位、信息和图像。我想更改名称为“测试”的信息文本,例如,在单击其他类的按钮后。我试图为这些自定义视图设置标签,但我还没有真正理解它是如何工作的。
PlantView.java
public class PlantView extends FrameLayout {
//Views
private UnderlinedTextView nameView;
private TextView infoView;
private TextView degreeView;
private CircleImageView imageView;
private LinearLayout planteBg;
//Attributes
private String nameText;
private String infoText;
private String degreeText;
private Drawable plantImage;
private boolean isExtended = false;
/****************
* CONSTRUCTEURS
***************/
/**
* Constructeur de la classe.
*
* @param context the context.
*/
public PlantView(@NonNull Context context) {
super(context);
obtainStyledAttributes(context, null, 0);
init();
}
/**
* Constructeur.
*
* @param context the context.
* @param attrs the attributes from the layout.
*/
public PlantView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
obtainStyledAttributes(context, attrs, 0);
init();
}
/**
* Constructeur.
*
* @param context the context.
* @param attrs the attributes from the layout.
* @param defStyleAttr the attributes from the default style.
*/
public PlantView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
obtainStyledAttributes(context, attrs, defStyleAttr);
init();
}
/**********
* FONCTIONS
***********/
//Fonction utile pour récupérer les attributs si l'on ajoute une "PlantView" directement en XML
private void obtainStyledAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
if (attrs != null) {
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlantView, defStyleAttr, 0);
nameText = typedArray.getString(R.styleable.PlantView_name);
infoText = typedArray.getString(R.styleable.PlantView_info);
degreeText = typedArray.getString(R.styleable.PlantView_degree);
plantImage = typedArray.getDrawable(R.styleable.PlantView_android_src);
}
}
//Fonction permettant d'initialiser la vue
private void init() {
inflate(getContext(), R.layout.plantview, this);
nameView = findViewById(R.id.nomPlante);
infoView = findViewById(R.id.info);
degreeView = findViewById(R.id.degree);
imageView = findViewById(R.id.image);
planteBg = findViewById(R.id.plante);
setupView();
}
//Fonction permettant d'"installer" la vue
private void setupView() {
nameView.setText(nameText);
infoView.setText(infoText);
degreeView.setText(degreeText +" °C");
imageView.setImageDrawable(plantImage);
planteBg.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
if(isExtended){
TransitionManager.beginDelayedTransition(planteBg, new TransitionSet()
.addTransition(new ChangeBounds()));
ViewGroup.LayoutParams params = planteBg.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
planteBg.setLayoutParams(params);
isExtended = false;
}else if(isExtended == false) {
TransitionManager.beginDelayedTransition(planteBg, new TransitionSet()
.addTransition(new ChangeBounds()));
ViewGroup.LayoutParams params = planteBg.getLayoutParams();
params.height = 500;
planteBg.setLayoutParams(params);
isExtended = true;
}
}
});
}
/**
* Fonctions permettant de changer les attributs
* de la vue par programmation
*/
public void setName(String name) {
nameView.setText(name);
}
public void setInfo(String info){
infoView.setText(info);
}
public void setDegree(String degree){
degreeView.setText(degree+" °C");
}
public void setImage(Bitmap image){
imageView.setImageBitmap(image);
}
解决方案
On your setters after you've set the text call invalidate();
public void setName(String name) {
nameView.setText(name);
invalidate();
}
public void setInfo(String info){
infoView.setText(info);
invalidate();
}
public void setDegree(String degree){
degreeView.setText(degree+" °C");
invalidate();
}
public void setImage(Bitmap image){
imageView.setImageBitmap(image);
invalidate();
}
推荐阅读
- node.js - 无法读取炮兵脚本文件中的 CSV 文件
- xamarin - 从流中保存图像并获取文件路径
- python - 如何修复此 Python ModuleNotFoundError
- xamarin - 如果创建了多个共享项目并被多个主项目使用,如何在 xamarin 中管理 dll?
- javascript - 相对元素顶部的绝对元素悬停和点击问题
- javascript - 在反应中调用 onchange 事件中的两个函数
- c# - 我可以防止私有变量被同一类的其他对象更改吗?
- javascript - 如何使用 Chromium Embedded Framework 将 JS 注入应用程序
- excel - Excel VBA - 从多张有条件的工作表中复制并放置在不同工作表中的某个单元格中
- django - Django Formset image.url 未填充