main.ml文件中的布局组件定义如下:
<LinearLayout
android:id="@+id/meeting_length_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginTop="5dp"
android:gravity="center" />
在类ConfigureStandupTimer中对UI进行初始化的时候实例化容器,并根据用户设置填充对象:private void initializeMeetingLength() {
/*实例化容器,并删除容器中的所有View对象,以便重新加载*/
ViewGroup meetingLengthContainer = (ViewGroup) findViewById(R.id.meeting_length_container);
meetingLengthContainer.removeAllViews();
View meetingLengthView = null;
if (Prefs.allowVariableMeetingLength(this)) {/*读取用户设置,根据设置初始化View*/
/*创建Text类型View组件*/
meetingLengthView = createMeetingLengthTextBox();
} else {
/*创建Spinner类型View组件*/
meetingLengthView = createMeetingLengthSpinner();
}
/*填充View对象到布局容器中*/
meetingLengthContainer.addView(meetingLengthView);
}
/*创建Text组件的具体实现函数,注意要把Spinner类型组件赋值为null */
private View createMeetingLengthTextBox() {
meetingLengthEditText = new EditText(this);
meetingLengthEditText.setGravity(Gravity.CENTER);
meetingLengthEditText.setKeyListener(new DigitsKeyListener());
meetingLengthEditText.setRawInputType(InputType.TYPE_CLASS_PHONE);
meetingLengthEditText.setLayoutParams(new LayoutParams(dipsToPixels(60), LayoutParams.WRAP_CONTENT));
meetingLengthEditText.setText(Integer.toString(meetingLength));
meetingLengthEditText.setLines(1);
meetingLengthSpinner = null;
return meetingLengthEditText;
}
/*创建Spinner组件的具体实现函数,注意要把Text类型组件赋值为null*/
private View createMeetingLengthSpinner() {
meetingLengthSpinner = new Spinner(this);
meetingLengthSpinner.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
meetingLengthSpinner.setPrompt(this.getString(R.string.length_of_meeting));
ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this,R.array.meeting_lengths, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
meetingLengthSpinner.setAdapter(adapter);
meetingLengthSpinner.setSelection(getSpinnerPositionFromMeetingLength(meetingLength));
meetingLengthEditText = null;
return meetingLengthSpinner;
}