Java 如何更改自定义属性的值?
我可以轻松更改TextView的“android:text”属性的值:Java 如何更改自定义属性的值?,java,android,android-attributes,Java,Android,Android Attributes,我可以轻松更改TextView的“android:text”属性的值: textTem = (TextView) findViewById(R.id.textTem); textTem.setText("ssss"); 但是我有一个带有自定义属性的自定义组件 我的自定义组件类: public class DayItem extends RelativeLayout { ... } 我的xml自定义组件:
textTem = (TextView) findViewById(R.id.textTem);
textTem.setText("ssss");
但是我有一个带有自定义属性的自定义组件
我的自定义组件类:
public class DayItem extends RelativeLayout {
...
}
我的xml自定义组件:
<com.example.a28210.weathpredict.DayItem
android:id="@+id/days1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
item:dayweather="snow"
item:daymaxtem="-2°"
item:daymintem="-12°"
item:daydate="12.8" />
更改视图的值使用自定义属性,而不是更改自定义属性的值 自定义属性用于从XML传递值以在自定义视图中使用。 例如:
someView.text = getString(R.styleable.DayItem_dayweather) //return "snow"
现在您想从运行时更改someView的值,只需向自定义视图添加一个方法:
public class DayItem extends RelativeLayout {
...
void setWeather(String value){
someView.text = value
}
...
}
那你可以
DayItem d = (DayItem)view.findViewById(R.id.days1);
d.setWeather("Sunny")
希望这对您有所帮助。“没有方法更改acustom属性的值”——然后添加一个。您编写了
com.example.a28210.weathpredict.DayItem
。您可以在其上添加方法。setText
方法不是根据XML定义自动创建的
public class DayItem extends RelativeLayout {
...
void setWeather(String value){
someView.text = value
}
...
}
DayItem d = (DayItem)view.findViewById(R.id.days1);
d.setWeather("Sunny")