Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 动态创建按钮的垂直滚动视图_Android_Scroll_Scrollview - Fatal编程技术网

Android 动态创建按钮的垂直滚动视图

Android 动态创建按钮的垂直滚动视图,android,scroll,scrollview,Android,Scroll,Scrollview,在动态创建的按钮中添加滚动视图时遇到问题。以下是我的xml代码 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="fill_parent" andro

在动态创建的按钮中添加滚动视图时遇到问题。以下是我的xml代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="27dp"
        android:background="@drawable/custom_border"
        android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="243dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignTop="@+id/linearLayout1"
        android:layout_marginTop="28dp"
        android:ems="10"
        android:hint="@string/hinttextitem" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="26dp"
        android:background="@drawable/button_shape"
        android:onClick="newonClick"
        android:text="click" />
  </LinearLayout>


     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linear1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:id="@+id/dynamic"
        android:background="@drawable/dynamic_border"
        android:orientation="vertical" >
      </LinearLayout>

</RelativeLayout>

这是我的java代码

public class MainActivity extends Activity {

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

public void newonClick(View V){
    LinearLayout layout=(LinearLayout)findViewById(R.id.linear1);
    LinearLayout dynamiclayout=(LinearLayout)findViewById(R.id.dynamic);
    dynamiclayout.removeAllViews();
    EditText edittext = (EditText) findViewById(R.id.editText1); 
    int x=Integer.valueOf(edittext.getText().toString());
    for(int i=1;i<=x;i++)
    {
      Button addButton =new Button(this);
      addButton.setText("Button"+i);
      addButton.setId(i);
      dynamiclayout.addView(addButton);
      addButton.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             Toast.makeText(getApplicationContext(),
                  "button is clicked"+v.getId(), 8000).show();
         }
      });
    }
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
公共void newonClick(视图五){
LinearLayout布局=(LinearLayout)findViewById(R.id.linear1);
LinearLayout dynamiclayout=(LinearLayout)findViewById(R.id.dynamic);
dynamicClayOut.removeAllViews();
EditText EditText=(EditText)findViewById(R.id.editText1);
int x=Integer.valueOf(edittext.getText().toString());

对于(int i=1;i请尝试以下操作:仅为LinearLayout放置滚动视图

 <ScrollView
    android:layout_width="wrap_content"
    android:layout_below="@+id/linear1"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/dynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:background="@drawable/dynamic_border"
        android:orientation="vertical" >
    </LinearLayout>
</ScrollView>


请替换此代码:

尝试使用, 在XML中


在Java中:

this.scrollView = (ScrollView) findViewById(R.id.scrollView1);
this.linearLayout = (LinearLayout) findViewById(R.id.dynamic);
this.linearLayout.setOrientation(LinearLayout.VERTICAL);
Button[] btn = new Button[10];
for (int i = 0; i < 10; i++) {
    btn[i] = new Button(this);
    btn[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    btn[i].setText("This is the button" + i);
   this.linearLayout.addView(btn[i]);
}
this.scrollView=(scrollView)findViewById(R.id.scrollView1);
this.linearLayout=(linearLayout)findViewById(R.id.dynamic);
此.linearLayout.setOrientation(linearLayout.VERTICAL);
按钮[]btn=新按钮[10];
对于(int i=0;i<10;i++){
btn[i]=新按钮(此按钮);
btn[i].setLayoutParams(新的LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容));
btn[i].setText(“这是按钮”+i);
此.linearLayout.addView(btn[i]);
}

您在哪里创建了scrollview?并且您没有从oncreate中调用newonClick(View v)方法:)谢谢大家..这对我有用..我需要的另一个帮助是如何在动态创建的按钮中添加gridview。现在我将按钮设置为垂直,我想将其显示为网格..使用网格视图而不是线性布局,并为网格功能维护一个适配器,您的动态按钮信息将传递给adaplter.@balaji.amndroid,你能告诉我如何将动态按钮信息传递给适配器吗?@ammu这是gridview和自定义适配器功能的简单示例,试试这个
<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linear1" 
        android:id="@+id/scrollView1">

<LinearLayout
        android:id="@+id/dynamic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="80dp"
        android:orientation="vertical" >
</LinearLayout>
</ScrollView>
this.scrollView = (ScrollView) findViewById(R.id.scrollView1);
this.linearLayout = (LinearLayout) findViewById(R.id.dynamic);
this.linearLayout.setOrientation(LinearLayout.VERTICAL);
Button[] btn = new Button[10];
for (int i = 0; i < 10; i++) {
    btn[i] = new Button(this);
    btn[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    btn[i].setText("This is the button" + i);
   this.linearLayout.addView(btn[i]);
}