2012-02-22 64 views
0

我想以编程方式制作此ViewGroup,但我遇到了麻烦(因为我对这些东西还不是很好)有人能指出如何以编程方式执行此操作吗?如何以编程方式从xml中精确制作视图

<TextView 
    android:text="Semester 1" 
    android:textColor="#b3000d" 
    android:gravity="center_vertical|center_horizontal" 
    android:textSize="26dip" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textStyle="bold" 
    android:background="#ffb0b6" 
    android:layout_marginBottom="5dip" 
    android:typeface="sans"/> 
<RelativeLayout 
    android:id="@+id/relative" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="#570000"> 
<TextView android:id="@+id/tv_1" 
    android:textColor="#FFFFFF" 
    android:gravity="center_vertical|left" 
    android:textSize="16dip" 
    android:layout_height="wrap_content" 
    android:textStyle="bold" 
    android:typeface="serif" 
    android:layout_width="90dip" 
    android:paddingLeft="20dip" 
    android:text="Grade"> 
</TextView> 
<TextView android:id="@+id/tv_2" 
    android:textColor="#FFFFFF" 
    android:gravity="center_vertical|left" 
    android:textSize="16dip" 
    android:layout_height="wrap_content" 
    android:textStyle="bold" 
    android:typeface="serif" 
    android:layout_width="200dip" 
    android:layout_toRightOf="@+id/tv_1" 
    android:text="Courses"> 
</TextView> 

</RelativeLayout> 
<ListView 
    android:id="@+id/lv_country" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:cacheColorHint="#00000000"> 
</ListView> 
+0

它缺少根(布局)元素。你是否故意忽略那部分? COS这是一个无效的XML .. – 2012-02-22 01:09:19

+0

是的。我故意留下了这个。如果真的很烦人,那么根就是一个LinearLayout,它的高度和宽度都是fill_parent。 – 2012-02-22 01:37:07

回答

1

下面的代码以编程方式在布局中构建一系列文本视图和旋转器。

private void addQuestionToLayout(final Question question, ViewGroup viewGroup) { 
    LinearLayout linearLayout = new LinearLayout(this); 
    linearLayout.setOrientation(LinearLayout.VERTICAL); 
    TextView textView = new TextView(this); 
    textView.setText(question.name); 
    linearLayout.addView(textView); 
    Spinner spinner = new Spinner(this); 
    linearLayout.addView(spinner); 
    questionToSpinner.put(question, spinner); 
    SpinnerAdapter adapter = new SpinnerAdapter(this, android.R.layout.simple_spinner_item, question); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_item); 
    spinner.setAdapter(adapter); 
    spinner.setSelection(question.defaultAnswer); // problems??? 
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      SpinnerAdapter spinnerAdapter = (SpinnerAdapter) parent.getAdapter(); 
      showToast(spinnerAdapter.question.name + " position=" + position + " id=" + id); 
      score.setText(model.formatScoreString(score())); 
     } 
     public void onNothingSelected(AdapterView<?> parent) { 
      SpinnerAdapter spinnerAdapter = (SpinnerAdapter) parent.getAdapter(); 
      showToast(spinnerAdapter.question.name + " unselected"); 
     } 
    }); 
    viewGroup.addView(linearLayout); 
} 
private void addQuestionsToLayout(Model model, ViewGroup viewGroup) { 
    for (Question question : model.questions) 
     addQuestionToLayout(question, viewGroup); 
} 
相关问题