-1

以下は、私のコードと xml ファイルです。2 つのフレーム レイアウト内に 2 つのテーブル レイアウトがあります。最初のテーブル レイアウトはテーブルの見出しを設定するために使用され、2 番目のテーブルは 2 番目のテーブル レイアウトで行を動的に設定するために使用されますが、見出しは完全に適合しているように見えますが、見出しの下に行が適切に配置されていない可能性があります。テーブル レイアウトのパラメータの設定に誤りがあります。私は修正中です。どんな助けでも大歓迎です。ありがとうございました。

package com.table;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.YuvImage;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import java.util.List;





public class TableActivity extends Activity 
{
    TableLayout table,table_values;
    Button scan,add;
    String value;



    EditText ed1;


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

        createTableLayout(); 
        addrows();


        scan = (Button)findViewById(R.id.read);
        if(scan != null) 
        {
            scan.setOnClickListener(new OnClickListener() {

                public void onClick(View v) 
                {

                }
            });
        }








        try 
        {
            scan.setOnClickListener(new View.OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {
                   setContentView(R.layout.code);
                    add=(Button)findViewById(R.id.button2);

                }
            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        try {
            add.setOnClickListener(new View.OnClickListener() 
            {

                @Override
                public void onClick(View v) 
                {


                }
            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }








    }




    public void createTableLayout()
     {
            table = (TableLayout) findViewById(R.id.tableLayout1);
          //  table.setBackgroundResource(R.drawable.sky);
            //TableRow tr_heading = new TableRow(this);
            table_values = (TableLayout) findViewById(R.id.tableLayout2);
            TableRow tr_heading = new TableRow(this);
            tr_heading.setId(10);
            tr_heading.setBackgroundColor(Color.GRAY);
            tr_heading.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.FILL_PARENT));



            TextView Serial = new TextView(this);
            Serial.setId(20);
            Serial.setText("Sr No.   ");
            Serial.setTextColor(Color.BLACK);
            tr_heading.addView(Serial); // add the column to the table row








            //TextView label_question = new TextView(this);
            TextView Name = new TextView(this);
            Name.setId(20);
            Name.setText("ID        ");
            Name.setWidth(40);
            Name.setTextColor(Color.BLACK);
            tr_heading.addView(Name); // add the column to the table row

            //TextView label_question = new TextView(this);
            TextView Quantity = new TextView(this);
            Quantity.setId(20);
            Quantity.setText("                             Quantity");
            Quantity.setTextColor(Color.BLACK);
            tr_heading.addView(Quantity); // add the column to the table row


            TextView amt = new TextView(this);
            amt.setId(20);
            amt.setText("            Amt");
            amt.setTextColor(Color.BLACK);
            tr_heading.addView(amt); 


            table.addView(tr_heading, new TableLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.FILL_PARENT));

     }   


            //--------------------------Adding Rows to your Table --------------------------------------

        public void addrows()
            {

                            Integer count = 0;
                            for (int count1 = 0; count1<3; count1++)
                             { 


                                TableRow tr = new TableRow(this);
                                if (count % 2 != 0)
                                tr.setId(100 + count);
                                tr.setClickable(true);

                                tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));


                                TextView sr = new TextView(this);
                                sr.setText("1");
                                //sr.setGravity(Gravity.CENTER );
                                sr.setPadding(2, 0, 5, 0);
                                sr.setTextColor(Color.WHITE);
                                sr.setClickable(true);
                                tr.addView(sr);








                                TextView idval = new TextView(this);

                                idval.setText(value);
                                idval.setGravity(Gravity.CENTER );
                                idval.setText("             fdfj");
                                idval.setPadding(2, 0, 5, 0);

                                idval.setTextColor(Color.WHITE);
                                idval.setClickable(true);
                                tr.addView(idval);


                                EditText quantity = new EditText(this);
                                //quantity.setId(200 + count);
                                quantity.setGravity(Gravity.CENTER );
                                quantity.setTextColor(Color.BLACK);
                        //      quantity.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                        //      quantity.setWidth(1);
                                quantity.setText("1");
                                quantity.setInputType(InputType.TYPE_CLASS_NUMBER);
                                int val=Integer.parseInt((quantity.getText().toString()));
                        //      quantity.setHeight(2);
                                quantity.setEnabled(true);
                                quantity.setPadding(2,0,5,0);
                                tr.addView(quantity);


                                TextView amount=new TextView(this);
                                amount.setGravity(Gravity.CENTER);
                                amount.setTextColor(Color.WHITE);
                                amount.setClickable(true);
                                //amount.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
                                amount.setEnabled(true);
                                //amount.setText("             1000");
                                amount.setText(String.valueOf((    val*10)));
                        //      amount.setWidth(1);
                        //      amount.setHeight(2);
                                tr.addView(amount);


                                // finally add this to the table row
                                table_values.addView(tr, new TableLayout.LayoutParams(
                                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                                count++;



                             }
            }




}



     **Xml file is below:
     <?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">
    <FrameLayout 
        android:id="@+id/frameLayout1" 
        android:layout_height="match_parent" 
        android:layout_width="match_parent">
    <FrameLayout 
        android:id="@+id/frameLayout2"  
        android:layout_height="match_parent" 
        android:layout_marginTop="40dp" 
        android:layout_marginBottom="70dp" 
        android:layout_width="match_parent">
            <TableLayout 
                android:layout_height="wrap_content" 
                android:id="@+id/tableLayout1" 
                android:layout_width="match_parent">
           </TableLayout>
    <FrameLayout
                android:id="@+id/frameLayout4" 
                android:layout_width="match_parent" 
                android:layout_height="match_parent">
         <TableLayout 
         android:layout_height="wrap_content" android:id="@+id/tableLayout2" android:layout_width="match_parent">
    </TableLayout>

        </FrameLayout>

    </FrameLayout>
    <FrameLayout android:id="@+id/frameLayout3" android:layout_height="wrap_content" android:layout_width="match_parent" android:layout_gravity="bottom">

        <Button 
            android:text="Scan" 
            android:id="@+id/button1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:layout_marginLeft="10dp">
        </Button>
        <Button 
            android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button"  android:layout_gravity="center_horizontal">
        </Button>

    </FrameLayout>
    </FrameLayout>

</LinearLayout>


screen shot:   http://i.stack.imgur.com/0nqsR.png 


![This is ther screen image[1]


  [1]: http://i.stack.imgur.com/0nqsR.png
4

1 に答える 1

1

あなたは2つのフレームレイアウトを持っているので、それらが他の下にあるようにするものは何も与えられていないので、互いに重なり合っています。

とにかく、単一の tableLayout 、または listView 、または gridLayout を使用することをお勧めします。

以下は tableLayout の例です。

http://wowjava.wordpress.com/2011/04/01/dynamic-tablelayout-in-android/

于 2012-07-07T09:32:57.780 に答える