【问题标题】:How to create Button Dynamically in android?如何在android中动态创建按钮?
【发布时间】:2013-02-26 06:03:44
【问题描述】:

我想创建一个这样的页面。这 7 个按钮已经存在,但如果用户想要添加更多类别(按钮),则可以使用 + 按钮并使用 - 按钮删除。 有什么想法或教程吗?

【问题讨论】:

    标签: java android android-layout android-intent android-button


    【解决方案1】:

    创建/删除按钮onClick+ button- button 如下:

      public void onClick(View v) {
    
         switch(v.getId()){
         case (R.id.plusbutton):
                     Button myButton = new Button(this);
                     myButton.setText("Add Me");
    
                     LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
                     LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                     ll.addView(myButton, lp);
                     break;.
         case (R.id.minusbutton):
                     Button myButton = new Button(this);
                     myButton.setText("Remove Me");
    
                     LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout);
                     LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                     ll.removeView(myButton, lp);
                     break;
               }
             }
    

    【讨论】:

    • 谢谢您,先生 :) 感谢您的帮助
    • 什么是按钮布局?如何创建它
    【解决方案2】:

    这是用于在android中动态创建按钮

    LinearLayout row2 = (LinearLayout) findViewById(R.id.hll2);
    Button ivBowl = new Button(this);
    ivBowl.setText("hi");
    LinearLayout.LayoutParams layoutParams = new  LinearLayout.LayoutParams(70, 70);
    layoutParams.setMargins(5, 3, 0, 0); // left, top, right, bottom
    ivBowl.setLayoutParams(layoutParams);
    row2.addView(ivBowl);
    

    【讨论】:

      【解决方案3】:
      LinearLayout mainLayout = (LinearLayout)findViewById(R.id.yourlayoutidthatisonethepicture);
      
      Button addButton =new Button(this);
      addButton.setText("add");
      
      mainLayout.addView(addButton);
      

      删除是一样的,只需将这个“mainLayout.addView(addButton)”更改为 removeView 或按钮的 setVisibility 为 View.GONE

      【讨论】:

      • 仍然没有得到“R.id.yourlayoutidthatisonethepicture”请告诉我
      • 我遇到了身高问题。我正在使用 addButton.setHeight(20) 但没有任何问题,如果你指导我为什么会这样?
      【解决方案4】:

      这很简单。

          Button button1=new Button(context);
          button1.setText("test");
          button1.setId(id);
      containerlayout.add(button1);
      

      希望对你有所帮助。

      【讨论】:

        【解决方案5】:

        如果您想创建动态视图(如 EditText、textview 等),只需使用此代码并在您的应用程序中运行它。

        MyActivity.java://你的 java 文件

         LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);
         EditText et = new EditText(v.getContext());
         et.setText("My new Edit Text);
         et.setMinLines(1);
         et.setMaxLines(3);
         ll.addView(et);
        

        在 XML 文件中:

         <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/TextView01"
        android:layout_below="@+id/relativeLayout1"
        android:orientation="vertical" >
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-09-05
          • 2016-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多