【发布时间】:2015-11-16 12:22:07
【问题描述】:
一个典型的 android 活动。
public class MainActivity extends AppCompatActivity {
/* Should I declare view components here? */
TextView textView;
Button button;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/* And then create them here */
textView = (TextView) findViewById(R.id.textview);
button = (Button) findViewById(R.id.button);
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
/* Or is it better to declare and create them like this? */
Spinner spinner = (Spinner) findViewById(R.id.spinner);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
}
}
在这两种情况下,组件都会按预期工作并且可以按预期使用。但是,在您的主要活动或片段中声明类似的视图时,是否应该遵循编程实践或模式?还是根本不重要。
【问题讨论】:
标签: android design-patterns view components declare