在ScrollView中添加一个按钮

时间:2014-02-03 12:47:57

标签: android button radio-button scrollview

我需要一些帮助我有一个滚动视图,用单选按钮显示一些问题,我想在用户到达最后一个问题时添加一个按钮,我希望只有在用户选择了所有答案后才能使用此按钮问题。

这是我的代码到目前为止,但它没有显示任何内容。我可以在没有按钮的情况下显示问题,但我也想显示按钮。

String countryName[] = { "India", "Pakistan", "China", "Nepal",
   "Bangladesh" };

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linear1);

      for (int k = 1; k <= 10; k++)       
      {
       //create text button
       TextView title = new TextView(this);
       title.setText("Question Number:" + k);
       title.setTextColor(Color.BLUE);
       mLinearLayout.addView(title);

       // create radio button
       final RadioButton[] rb = new RadioButton[5];
       RadioGroup rg = new RadioGroup(this);
       rg.setOrientation(RadioGroup.VERTICAL);

       for (int i = 0; i < 5; i++) 
       {
        rb[i] = new RadioButton(this);
        rg.addView(rb[i]);
        rb[i].setText(countryName[i]);       
       }
       mLinearLayout.addView(rg);}

      ScrollView scroll = new ScrollView(this);

      Button btn = new Button(this);
      ViewGroup.LayoutParams blp = new ViewGroup.LayoutParams(
              LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
      btn.setLayoutParams(blp);
      btn.setText("Click Me");

      mLinearLayout.addView(scroll);
      mLinearLayout.addView(btn);

      setContentView(mLinearLayout);
}

XML文件:

 <!--?xml version="1.0" encoding="utf-8"?-->
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/buttons"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_alignParentBottom="true">       
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/textView3"
        android:text="Continue" />

 </LinearLayout>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<LinearLayout 
    android:id="@+id/linear1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical">
</LinearLayout>

</ScrollView>

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

    mLinearLayout.addView(btn);
          scroll.addView(mLinearLayout);
setContentView(scroll);

答案 1 :(得分:0)

您应该将LinearLayout放在ScrollView中,因为ScrollView是一个容器,其子项是可滚动的。另外,将ScrollView设置为活动的内容视图并定义其布局参数。

ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
scroll.addView(mLinearLayout);
setContentView(scroll); 

答案 2 :(得分:0)

您好我找到了正确的方法!最简单的方法是修复XML文件并在LinearLayout中添加LinearLayout,并将问题放在内部LinearLayout中,将按钮放在外部LinearLayout中。

这是诀窍:

<!--?xml version="1.0" encoding="utf-8"?-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="vertical" >

 <LinearLayout
     android:id="@+id/linear1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="2.87"
     android:orientation="vertical" >
 </LinearLayout>

 <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="2.87"
     android:text="Button" />

 </LinearLayout>

</ScrollView>
相关问题