当我尝试添加更多时,按钮不断消失

时间:2017-03-25 06:23:58

标签: java android

所以基本上我的程序应该在每次单击另一个按钮时在单独的活动中创建一个按钮。但是每次我添加一个按钮时,我添加的上一个按钮都会消失(按钮会水平移动)。

insert into assignment(`col1`,`col2`,`col3`,`col4`,`col5`, `col6`,`col7`,`col8`) values('$rcptno','$subdt','$amarks','$Vvmarks','$chk_dt', '$roll_no','$sbcode','$ecode')

3 个答案:

答案 0 :(得分:0)

那是因为你要将按钮添加到另一个的相同位置。你的按钮没有消失,只是你在前一个按钮上呈现新按钮。

您可以通过多种方式正确地完成此操作,例如一个接一个地更改新按钮的位置,例如保持水平位置相同并更改垂直位置(确保您的布局中有滚动视图)。

或者,使用RecyclerView,其中孩子只有一个按钮。每次需要新按钮时,只需在RecyclerView底部添加一个新子项。

答案 1 :(得分:0)

试试这会对你有用

将其复制到xml布局文件中

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ll_main">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/click"
    android:text="click"/>
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


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

</LinearLayout>

这是MainActivity.java

public class MainActivity extends Activity {

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

    final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_add_btn);
    final Button button = (Button) findViewById(R.id.click);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            LinearLayoutCompat.LayoutParams lparams = new LinearLayoutCompat.LayoutParams(
                    LinearLayoutCompat.LayoutParams.WRAP_CONTENT, LinearLayoutCompat.LayoutParams.WRAP_CONTENT);
            Button button1 = new Button(getApplicationContext());
            button1.setLayoutParams(lparams);
            button1.setText("added");
            linearLayout.addView(button1);
        }
    });

}

}

答案 2 :(得分:0)

我检查了你的代码,你只在代码中添加了一个按钮。 并且for循环被削减,此时无法运行。 此外,如果您打开循环,则使用相对布局。所以在这种情况下,你必须设置按钮左或右对象id。 请尝试这样。

  1. 请删除斜杠并使用for循环。 请将RelativeLayout更改为LinearLayout。
  2. 如果不起作用,请更改现场默认值。 我从未将对象id用作0,所以不确定它是否可行。