-1

目前我的代码显示线性布局 在此处输入图像描述

但我想这样显示 在此处输入图像描述

在这里我附上我的代码请尝试帮助

public class MainActivity extends Activity 
{
    LinearLayout MainLinearObj;

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

        MainLinearObj = (LinearLayout) findViewById(R.id.MainLinear);

        for(int i = 0; i < 3; i++) 
        {
            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
            llp.setMargins(10, 10, 10, 10);
            llp.weight = 1;

            LinearLayout ll = new LinearLayout(MainActivity.this); 
            ll.setBackgroundColor(Color.RED);
            ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
            ll.setId(i);
            MainLinearObj.addView(ll,llp);
        }
    }
}

user1566160 的编辑代码检查它并查看它的输出,如果我在 edittext 中输入 10,那么我需要总共 10 个正方形,因为每行的输出需要 3 个正方形

public class MainActivity extends Activity 
{
    LinearLayout MainLinearObj;
    EditText et;
    Button btn;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MainLinearObj = (LinearLayout) findViewById(R.id.MainLinear);
        et = (EditText) findViewById(R.id.editText1);
        btn = (Button) findViewById(R.id.button1);

//      String str  = et.getText().toString();
//      int no = Integer.parseInt(str);
        btn.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View v) 
            {
                String str  = et.getText().toString();
                int no = Integer.parseInt(str);

                // TODO Auto-generated method stub
                for(int j=0;j<no;j++) 
                {
                    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
                    llp.setMargins(10, 10, 10, 10);
                    llp.weight = 1;
                    LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
                    LinearLayout newLinearLayout = new LinearLayout(MainActivity.this);
                    newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
                    newLinearLayout.setLayoutParams(llp1);
                    for(int i = 0; i < 3; i++) 
                    {

                        LinearLayout ll = new LinearLayout(MainActivity.this); 
                        ll.setOrientation(LinearLayout.VERTICAL);
                        ll.setBackgroundColor(Color.RED);
                        ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
                        ll.setId(i);
                        newLinearLayout.addView(ll,llp);
                    }
                    MainLinearObj.addView(newLinearLayout);
                }
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="3" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="Selected Table "
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:text=":-"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblChkTabDisSelTable"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="Cover Require"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:text=":-"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblChkTabDisReqCover"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3" >

            <TextView
                android:id="@+id/textView4"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:text="Selected Cover"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:text=":-"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@+id/lblChkTabDisToCover"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="3"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/btnChkTabAll"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="All" />

        <Button
            android:id="@+id/btnChkTabAC"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="A/c" />

        <Button
            android:id="@+id/btnChkTabNAC"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="3"
            android:text="Non A/c" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="10dp"
        android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        >

        <TextView
            android:id="@+id/lblChkTabDiaName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Table Name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/lblChkTabDiaCover"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Table Cover"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/lblChkTabDiaNote"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Table Note"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/fglblDiaState"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Remark"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>



    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="1"
      >

          <ScrollView
              android:id="@+id/scrollView1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_weight="1" >

        <LinearLayout
            android:id="@+id/llChkTabDisp"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:background="#CCCCCC"
            android:orientation="vertical" >

        </LinearLayout>
    </ScrollView>

          <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical" >

              <LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="4" >

              </LinearLayout>

              <LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="6" >

              </LinearLayout>

              <LinearLayout
                  android:layout_width="fill_parent"
                  android:layout_height="match_parent"
                  android:layout_weight="4" >

                  <Button
                      android:id="@+id/btnChkTabDiaOk"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:enabled="false"
                      android:text="Ok" />

                  <Button
                      android:id="@+id/btnChkTabDiaCancel"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:layout_weight="1"
                      android:text="Cancel" />

              </LinearLayout>

          </LinearLayout>

    </LinearLayout>
</LinearLayout>
</LinearLayout><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"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:context=".MainActivity" >


    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/MainLinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    </LinearLayout>

</LinearLayout>
4

1 回答 1

1

您可以尝试以下方法:

        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout MainLinearObj = (LinearLayout) findViewById(R.id.abc);

        for(int j=0;j<3;j++) 
        {
            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
            llp.setMargins(10, 10, 10, 10);
            llp.weight = 1;
            LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            LinearLayout newLinearLayout = new LinearLayout(this);
            newLinearLayout.setOrientation(LinearLayout.VERTICAL);
            newLinearLayout.setLayoutParams(llp1);
            for(int i = 0; i < 3; i++) 
            {

                LinearLayout ll = new LinearLayout(MainActivity.this); 
                ll.setOrientation(LinearLayout.HORIZONTAL);
                ll.setBackgroundColor(Color.RED);
                ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
                ll.setId(i);
                newLinearLayout.addView(ll,llp);
            }
            MainLinearObj.addView(newLinearLayout);
        }
}

编辑:

在 XML 中将 Main Linear Layout 设置为 Vertical 方向,然后使用以下代码:

LinearLayout MainLinearObj = (LinearLayout) findViewById(R.id.abc);

    for(int j=0;j<10;j++) 
        {
            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
            llp.setMargins(10, 10, 10, 10);
            llp.weight = 1;
            LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            LinearLayout newLinearLayout = new LinearLayout(this);
            newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            newLinearLayout.setLayoutParams(llp1);
            for(int i = 0; i < 3; i++) 
            {

                LinearLayout ll = new LinearLayout(MainActivity.this); 
                ll.setOrientation(LinearLayout.VERTICAL);
                ll.setBackgroundColor(Color.RED);
                ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
                ll.setId(i);
                newLinearLayout.addView(ll,llp);
            }
            MainLinearObj.addView(newLinearLayout);
        }

编辑2

int no = Integer.parseInt(str);;
        int count = 0;
        int myvar = (no/3) +1;
        for(int j=0;j<myvar;j++) 
        {
            LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(100,100);
            llp.setMargins(10, 10, 10, 10);
            llp.weight = 1;
            LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            LinearLayout newLinearLayout = new LinearLayout(this);
            newLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            newLinearLayout.setLayoutParams(llp1);
            for(int i = 0; i < 3; i++) 
            {

                LinearLayout ll = new LinearLayout(MainActivity.this); 
                ll.setOrientation(LinearLayout.VERTICAL);
                ll.setBackgroundColor(Color.RED);
                ll.setLayoutParams(new LinearLayout.LayoutParams(100,100));
                ll.setId(i);
                count++;
                if(count<=no)
                newLinearLayout.addView(ll,llp);


            }
            MainLinearObj.addView(newLinearLayout);
            if(count==no) {
                break;
            }
        }
于 2013-03-19T10:42:09.557 回答