简体   繁体   中英

Dynamically adding buttons and layout in android

Here is the code using layout resource file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:clipChildren="false"
          android:clipToPadding="false"
          tools:context=".MainActivity">


<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:clipChildren="false"
    android:clipToPadding="false"
    >

    <Button
        android:id="@+id/im11"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:src="@drawable/munshee_logo"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"/>

    <Button
        android:id="@+id/im12"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"

        android:src="@drawable/munshee_logo" />

    <Button
        android:id="@+id/im13"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />

    <Button
        android:id="@+id/im14"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />
    <Button
        android:id="@+id/im15"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:padding="1dp"
        android:scaleType="centerCrop"
        android:cropToPadding="true"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="@drawable/munshee_logo"
        android:src="@drawable/munshee_logo" />


</LinearLayout>

how do I convert this into java code? The outcome should be one row of buttons horizontally spread across the screen. also, each button should scale itself according to the dimensions of the screen. Please help me implement this.

Adding views and components on layout in java is mostly similar to xml. Simple Example method is below:-

public LinearLayout createRow() {
        LinearLayout objLinearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams objLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        objLinearLayout.setWeightSum(3);
        objLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
        objLinearLayout.setLayoutParams(objLayoutParams);

        Button objButton = new Button(mContext);
        LinearLayout.LayoutParams objButonLayoutParams = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
        objButonLayoutParams.weight = 1;
        objButton.setText("Add Button");
        objButton.setBackgroundColor(Color.LTGRAY);
        objButton.setTextColor(Color.BLACK);
        objButton.setLayoutParams(objButonLayoutParams);

        objLinearLayout.addView(objButton);

        /*
        * Here you can add other views like Textview,Spinner,etc
        * Every components has same method like in xml.*/


        return objLinearLayout;//This layout can display where you want.
    }

You can try this, Add buttons dynamically:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context="com.xanadutec.testviews.MainActivity">

</LinearLayout>

In Java Class:

HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
LinearLayout ll = new LinearLayout(this);
horizontalScrollView.addView(ll);
ll.setOrientation(LinearLayout.HORIZONTAL);

for(int i = 0; i < 20; i++) {
   Button cb = new Button(this);
   cb.setText("I'm dynamic!");
   ll.addView(cb);
 }

  this.setContentView(horizontalScrollView);

EDITED:

ScrollView scrollView =new ScrollView(this);
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        scrollView.addView(layout);
        for (int i = 0; i < 10; i++) {
            LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

            for (int j = 0; j < 3; j++) {
                Button btnTag = new Button(this);
                btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                btnTag.setText("Button " + (j + 1 + (i * 3)));
                btnTag.setId(j + 1 + (i * 3));
                row.addView(btnTag);
            }

            layout.addView(row);
        }
        super.setContentView(scrollView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM