我想改变膨胀的子视图组pi不知道如何访问widget。
public class MainActivity extends AppCompatActivity {
ScrollView activitymain;
LinearLayout rootLayout, subInfo;
TextView tvSerName, tvSerPrice, tvStarDate, tvNextDate;
CreateSubActivity createSubActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootLayout = (LinearLayout) findViewById(R.id.root);
subInfo = (LinearLayout) findViewById(R.id.subinfo);
activitymain = (ScrollView) findViewById(R.id.activity_main);
tvSerName = (TextView) findViewById(R.id.tvSerName); //these are child of subinfo
tvSerPrice = (TextView) findViewById(R.id.tvSerPrice);
tvStarDate = (TextView) findViewById(R.id.tvStarDate);
tvNextDate = (TextView) findViewById(R.id.tvNextDate);
}
当在另一个活动中的按钮被按下时,这个方法就会起作用。当视图被添加时,我想通过setText()来改变tvSerName, tvSerPrice, tvStarDate, tvNextDate的文本,但它不起作用。
void addView() {
createSubActivity = new CreateSubActivity();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup addview = (ViewGroup) inflater.inflate(R.layout.subinfo, null);
tvSerName.setText("name"); // it does not works
rootLayout.addView(addview);
}
你试图设置文本的视图还没有在viewGroup中设置。
你仍然需要在视图组中设置 tvSerName = addview.findVieById(R.id.tvSerName);
在其上设置文字之前。
void addView() {
createSubActivity = new CreateSubActivity();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup addview = (ViewGroup) inflater.inflate(R.layout.subinfo, null);
//here
tvSerName = addview.findVieById(R.id.tvSerName);
//then set its text
tvSerName.setText("name"); // it does not works
rootLayout.addView(addview);
}