在我的活动中,我有时需要更改布局,还必须更改选项菜单。
So how can i know what is the current layout?
答案 0 :(得分:1)
使用活动/片段中的数据成员/每次“更改布局”时更新的内容,自行跟踪。
答案 1 :(得分:0)
活动类的示例代码 -
public class DynamicLayoutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This will create the LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Configuring the width and height of the linear layout.
LayoutParams llLP = new LayoutParams(
//android:layout_width='match_parent' an in xml
LinearLayout.LayoutParams.MATCH_PARENT,
//android:layout_height='wrap_content'
LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(llLP);
TextView tv = new TextView(this);
LayoutParams lp = new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
//android:text='@string/c4r'
tv.setText(R.string.c4r);
//android:padding='@dimen/padding_medium'
tv.setPadding(8, 8, 8, 8);
ll.addView(tv);
EditText et = new EditText(this);
et.setLayoutParams(lp);
et.setHint(R.string.c4r);
et.setPadding(8, 8, 8, 8);
ll.addView(et);
Button bt = new Button(this);
bt.setText(R.string.OtherActivity);
bt.setPadding(8, 8, 8, 8);
ll.addView(bt);
//Now finally attach the Linear layout to the current Activity.
setContentView(ll);
//Attach OnClickListener to the button.
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),
'This is dynamic activity', Toast.LENGTH_LONG).show();
}
});
}
}
并在manifest.xml中添加 -
<activity android:name='.DynamicLayoutActivity'
android:label='@string/dynamic_layout_activity'>
<intent-filter >
<category android:name='android.intent.category.LAUNCHER'/>
</intent-filter>
</activity>
答案 2 :(得分:0)
尝试这样做:
为您的布局提供ID
使用findViewById方法
找到它
然后您可以添加视图
。