将contentView设置为布局后,如何通过java管理UI?另外,如果我开始一项新活动,我是否重置了contentView?
答案 0 :(得分:2)
ContentView只会在一个活动中。
假设您处于扩展Activity
所需的全部内容之后,请在设置内容视图后访问您的观看次数。假设您的布局中有一个按钮:
Button homeButton = (Button) findViewById(R.id.homeButton);
homeButton.setText(); homeButton.setOnClickListener() etc
如果您正在进行对话,或者需要在特定情况下访问某些视图,或者您有意图且需要访问主要活动布局中的内容:
Button secondButton = (Button) getActivity().findViewById(R.id.secondButton);
//other methods on the button
编辑 -
好的图片,您只需将ImageView添加到布局中:
<ImageView android:id="@+id/logoImageView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/logo" android:contentDescription="@string/app_name"
android:layout_gravity="center_horizontal"/>
您不需要专门对代码执行任何操作,它应该只显示。
也可以通过XML添加按钮和其他视图。您也可以通过编程方式添加内容,但这并不常见。
如果我没记错的话,用Java创建一个不在XML文件中的按钮的构造函数就是Button aNewButton = new Button(getApplicationContext());
aNewButton.setText("whatever");