我从另一个活动创建了这个嵌套视图。值(要显示的文本、背景颜色等)主要由用户从其他活动输入。下面是我如何动态创建视图的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// Create a New Linear Layout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
float scale = getResources().getDisplayMetrics().density;
//Set Bottom Margin
float margin = 5; //RESIZE BOTTOM MARGIN HERE!
int margs = (int) (margin * scale + 0.5f);
//Set padding in dp
float padding = 5; //RESIZE PADDING HERE!
int pads = (int) (padding * scale + 0.5f);
llParams.setMargins(0,0,0,margs);
//Set Parameters for Android
ll.setLayoutParams(llParams);
ll.setPadding(pads, pads, pads, pads);
//Set Background Color on Layout
String chosenColor = data.getStringExtra("sendChosenColor");
if (chosenColor.equals("Green")){
ll.setBackgroundResource(R.color.HoloGreen);
}else if (chosenColor.equals("Blue")){
ll.setBackgroundResource(R.color.HoloBlue);
}else if (chosenColor.equals("Gray")){
ll.setBackgroundResource(R.color.HoloGray);
}else if (chosenColor.equals("Orange")){
ll.setBackgroundResource(R.color.HoloOrange);
}else {
ll.setBackgroundResource(R.color.HoloYellow);
}
//Adding Layout to Appropriate Container
int layoutCountLeft = subjectLeft.getChildCount();
int layoutCountRight = subjectRight.getChildCount();
if (layoutCountLeft <= layoutCountRight){
subjectLeft.addView(ll);
} else {
subjectRight.addView(ll);
}
//Create TextView for SubjectName
TextView SubjectName = new TextView(this);
SubjectName.setText(data.getStringExtra("sendSubjectName"));
SubjectName.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
SubjectName.setTextSize(22);
SubjectName.setTypeface(Typeface.DEFAULT_BOLD);
//Create TextView for SubjectNumber
TextView SubjectNumber = new TextView(this);
SubjectNumber.setText(data.getStringExtra("sendSubjectNumb"));
SubjectNumber.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
SubjectNumber.setTextSize(16);
//Creating the divider line
ImageView divider = new ImageView(this);
LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2);
divider.setLayoutParams(dividerParams);
divider.setBackgroundResource(R.color.Black);
//Add Views into the Layout
ll.addView(SubjectNumber);
ll.addView(SubjectName);
ll.addView(divider);
}
}
}
我可以在不使用数据库的情况下保存它吗?