How to add a button dynamically in Android?

后端 未结 17 1124
礼貌的吻别
礼貌的吻别 2020-11-22 13:17

How to add a button dynamically in Android?

相关标签:
17条回答
  • 2020-11-22 13:30

    try this:

    for (int i = 1; i <= 20; i++) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        Button btn = new Button(this);
        btn.setId(i);
        final int id_ = btn.getId();
        btn.setText("button " + id_);
        btn.setBackgroundColor(Color.rgb(70, 80, 90));
        linear.addView(btn, params);
        btn1 = ((Button) findViewById(id_));
        btn1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Toast.makeText(view.getContext(),
                        "Button clicked index = " + id_, Toast.LENGTH_SHORT)
                        .show();
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-22 13:33
    public void add_btn() {
    
        lin_btn.setWeightSum(3f);
        for (int j = 0; j < 3; j++) {
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params1.setMargins(10, 0, 0, 10);
            params1.weight = 1.0f;
    
            LinearLayout ll;
            ll = new LinearLayout(this);
            ll.setGravity(Gravity.CENTER_VERTICAL);
            ll.setOrientation(LinearLayout.HORIZONTAL);
            ll.setLayoutParams(params1);
    
            final Button btn;
            btn = new Button(DynamicActivity.this);
    
            btn.setText("A"+(j+1));
            btn.setTextSize(15);
            btn.setId(j);
            btn.setPadding(10, 8, 10, 10);
    
            ll.addView(btn);
    
            lin_btn.addView(ll);
    
    
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    if(v.getId()==0)
                    {
                        txt_text.setText("Hii");
                    }else if(v.getId()==1)
                    {
                        txt_text.setText("hello");
                    }else if(v.getId()==2)
                    {
                        txt_text.setText("how r u");
                    }
    
    
    
                }
            });
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 13:33

    In mainactivity.xml write:

    <Button
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search" 
        android:visibility="invisible"/>
    

    In main.java write:

    Button buttonSearch;
    buttonSearch = (Button)findViewById(R.id.search);
    buttonSearch.setVisibility(View.VISIBLE);
    
    0 讨论(0)
  • 2020-11-22 13:35

    You could create a base layout for your button and dynamically change only what is specific, like this project I made to run different exercises from a Material Design course I'm taking:

    In this example, I use a preconfigured AppCompatButton:

    layout_base_button.xml

    <android.support.v7.widget.AppCompatButton
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/btn_base"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="8dp"
        style="@style/RaisedButton"
        >
    
    </android.support.v7.widget.AppCompatButton>
    
    
    <style name="RaisedButton" parent="Widget.AppCompat.Button.Colored">
        <item name="android:textSize">11sp</item>
        <item name="android:textStyle">bold</item>
    </style>
    

    And in the MainActivity I created some instances and changed what I need, like the button text and onClick event:

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="udemy.android.materialdesign.MainActivity">
    
        <LinearLayout
            android:id="@+id/base_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
    
        </LinearLayout>
    
    
    </ScrollView>
    
    
    
    public class MainActivity extends AppCompatActivity {
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            LinearLayout baseLayout = findViewById(R.id.base_layout);
    
            baseLayout.addView(createButton("TextFields", baseLayout,
                    view -> startActivity(createIntent(TextFieldsActivity.class))
            ));
    
            baseLayout.addView(createButton("Buttons", baseLayout,
                    view -> startActivity(createIntent(ButtonsActivity.class))
            ));
    
            baseLayout.addView(createButton("Toolbar", baseLayout,
                    view -> startActivity(createIntent(ToolbarActivity.class))
            ));
    
        }
    
        private View createButton(String text, LinearLayout baseLayout, View.OnClickListener onClickEvent) {
            View inflated = LayoutInflater.from(this).inflate(R.layout.layout_base_button, baseLayout, false);
            AppCompatButton btnBase = inflated.findViewById(R.id.btn_base);
    
            btnBase.setText(text);
            btnBase.setOnClickListener(onClickEvent);
            return btnBase;
        }
    
        private Intent createIntent(Class<?> cls) {
            return new Intent(this, cls);
        }
    }
    

    Sorry for being late...

    0 讨论(0)
  • 2020-11-22 13:36
    for (int k = 1; k < 100; k++) {
        TableRow row = new TableRow(this);
    
        innerloop:
        for (int l = 1; l < 4; l++) {
            btn = new Button(this);
            TableRow.LayoutParams tr = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            layout.setWeightSum(12.0f);
            tr.weight = 0;
            btn.setLayoutParams(tr); 
            btn.setTextColor(a);
            btn.setHeight(150);
    
            btn.setWidth(150);
            btn.setId(idb);
            btn.setText("Button " + idb);
            row.addView(btn);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 13:39

    Try this:

    LinearLayout ll = (LinearLayout)findViewById(R.id.layout);
    
    Button btn = new Button(this);
    btn.setText("Manual Add");
    btn.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    ll.addView(btn);
    
    0 讨论(0)
提交回复
热议问题