Программно дать пространство между кнопками в макете таблицы

У меня есть такая деятельность.

![Изображение][1]

Здесь начинаются, завершаются и доставляются кнопки.how to give space between buttons.?

Ниже мой код.

у меня есть макет таблицы, разработанный в .xml, и, поскольку его данные являются динамическими, я добавляю строки программно.

/*** xml code ****/

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

     <ImageView         android:id="@+id/title_image"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/timg"/>

    <TextView   android:id="@+id/title"
                        android:layout_width="fill_parent"
                        android:layout_height="63dp"
                        android:text="Alpha Restaurant"
                        android:gravity="center_horizontal"
                        android:layout_toRightOf="@+id/title_image"
                        android:background="#ED4040"
                        android:typeface="serif"
                        android:textColor="#FFF"
                        android:textStyle="bold"
                        android:textSize="35sp"/>

      <TextView         android:id="@+id/tbno"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/title"
                        android:text="Kitchen Dashboard  :"
                        android:layout_marginLeft="20dp"
                        android:layout_marginTop="20dp"
                        android:typeface="serif"
                        android:textColor="#05539C"
                        android:textStyle="bold"
                        android:textSize="18sp"/>

     <TextView  android:id="@+id/tvtbno"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/title"
                        android:layout_toRightOf="@+id/tbno"
                        android:text="KIT01"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="20dp"
                        android:typeface="serif"
                        android:textColor="#05539C"
                        android:textStyle="bold"
                        android:textSize="18sp"/>

     <TextView  android:id="@+id/time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/title"
                        android:text="Time :"
                        android:layout_toRightOf="@+id/tvtbno"
                        android:layout_marginLeft="60dp"
                        android:layout_marginTop="20dp"
                        android:typeface="serif"
                        android:textColor="#05539C"
                        android:textStyle="bold"
                        android:textSize="18sp"/>

     <TextView  android:id="@+id/tvtime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/title"
                        android:layout_toRightOf="@+id/time"
                        android:text="KIT01"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="20dp"
                        android:typeface="serif"
                        android:textColor="#05539C"
                        android:textStyle="bold"
                        android:textSize="18sp"/>

     <TextView  android:id="@+id/date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/title"
                        android:text="Date :"
                        android:layout_toRightOf="@+id/tvtime"
                        android:layout_marginLeft="60dp"
                        android:layout_marginTop="20dp"
                        android:typeface="serif"
                        android:textColor="#05539C"
                        android:textStyle="bold"
                        android:textSize="18sp"/>

     <TextView  android:id="@+id/tvdate"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/title"
                        android:layout_toRightOf="@+id/date"
                        android:text="KIT01"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="20dp"
                        android:typeface="serif"
                        android:textColor="#05539C"
                        android:textStyle="bold"
                        android:textSize="18sp"/>

     <ScrollView        android:layout_width="fill_parent"
                                android:layout_height="370dp"
                                android:id="@+id/sc" android:layout_below="@+id/tvdate">

    <TableLayout  android:id="@+id/tableLayout1"
                          android:layout_below="@+id/tbno"
                          android:layout_marginTop="20dp"
                  android:layout_width="wrap_content"  
                  android:layout_height="550dp"  
                  android:shrinkColumns="*"  
                  android:stretchColumns="*">  

    <TableRow  
                  android:id="@+id/tableRow1"  
                  android:background="#A9D0F5"
                  android:layout_height="wrap_content"  
                  android:layout_width="match_parent">  

    <TextView  
                  android:id="@+id/tv1"  
                  android:text="Lot No"
                  android:layout_marginLeft="15dp"
                  android:textSize="20dp"
                  android:textStyle="bold"
                  android:textColor="#190710"
                  android:typeface="serif"/>
    <TextView  
                  android:id="@+id/tv2"  
                  android:text="Item"
                  android:layout_marginLeft="40dp"
                  android:textSize="20dp"
                  android:textStyle="bold"  
                  android:textColor="#190710"
                  android:typeface="serif"/>  

    <TextView  
                  android:id="@+id/tv3"  
                  android:text="Quantity"
                  android:layout_marginLeft="40dp"
                  android:textSize="20dp"
                  android:textStyle="bold"  
                  android:textColor="#190710"
                  android:typeface="serif"/>

    <TextView  
                  android:id="@+id/tv4"  
                  android:text="Order"
                  android:layout_marginLeft="40dp"
                  android:textSize="20dp"
                  android:textStyle="bold"  
                  android:textColor="#190710"
                  android:typeface="serif"/>
    <TextView  
                  android:id="@+id/tv5"  
                  android:text="Notes"
                  android:layout_marginLeft="40dp"
                  android:textSize="20dp"
                  android:textStyle="bold"  
                  android:textColor="#190710"
                  android:typeface="serif"/>

    <TextView  
                  android:id="@+id/tv6"  
                  android:text="Start"
                  android:layout_marginLeft="40dp"
                  android:textSize="20dp"
                  android:textStyle="bold"  
                  android:textColor="#190710"
                  android:typeface="serif"/>

    <TextView  
                  android:id="@+id/tv7"  
                  android:text="Completed"
                  android:layout_marginLeft="40dp"
                  android:textSize="20dp"
                  android:textStyle="bold"  
                  android:textColor="#190710"
                  android:typeface="serif"/>

    <TextView  
                  android:id="@+id/tv8"  
                  android:text="Delivered"
                  android:layout_marginLeft="40dp"
                  android:textSize="20dp"
                  android:textStyle="bold"  
                  android:textColor="#190710"
                  android:typeface="serif"/>
    </TableRow>  

    </TableLayout>
    </ScrollView>
    <!-- <TextView      android:id="@+id/total"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Total : "
                        android:textSize="22sp"
                        android:typeface="serif"
                        android:textStyle="bold"
                        android:layout_marginLeft="230dp"
                        android:layout_below="@+id/tableLayout1"/>

    <TextView   android:id="@+id/tvtotal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="500 Rs"
                        android:textSize="22sp"
                        android:typeface="serif"
                        android:textStyle="bold"
                        android:layout_toRightOf="@+id/total"
                        android:layout_below="@+id/tableLayout1"/>-->

    <Button     android:id="@+id/refresh"
                        android:layout_width="150dp"
                        android:layout_height="wrap_content"
                        android:text="Refresh"
                        android:textStyle="bold"
                        android:layout_marginTop="20dp"
                        android:layout_marginLeft="340dp"
                        android:layout_below="@+id/sc"/>

     <Button    android:id="@+id/home"
                        android:layout_width="150dp"
                        android:layout_height="wrap_content"
                        android:text="Home"
                        android:layout_marginLeft="20dp"
                        android:textStyle="bold"
                        android:layout_marginTop="20dp"
                        android:layout_toRightOf="@+id/refresh"
                        android:layout_below="@+id/sc"/>  

</RelativeLayout>


/**** java code ****/

@Override
                protected void onPostExecute(Boolean a) {
                        // TODO Auto-generated method stub
                        super.onPostExecute(a);
                        progressDialog.dismiss();
                        int count= 0;

                        try{
                        allarray = new JSONArray(result);
                        System.out.println("array is " +allarray.length());
                        JSONObject json_data=null;
                                for(int j=0;j<allarray.length();j++){
                                json_data = allarray.getJSONObject(j);
                                json_data.getString("PreparationLot");
                                json_data.getString("Prep_MenuItem");
                                json_data.getString("Prep_Quantity");
                                json_data.getString("Prep_Order_Number");
                                json_data.getString("Prep_Notes");

                                TableLayout tl = (TableLayout) findViewById(R.id.tableLayout1);

                        TableRow tr = new TableRow(DashBoard.this);
                        if(count%2!=0) {
                                tr.setBackgroundColor(Color.rgb(222, 219, 219));
                        }else{
                                tr.setBackgroundColor(Color.rgb(255, 255, 255));
                        }
                        tr.setId(20);
                        tr.setLayoutParams(new LayoutParams());

                        final TextView lotno = new TextView(DashBoard.this);
                        lotno.setId(200+count);// define id that must be unique
                        lotno.setText(""+json_data.getString("PreparationLot")); // set the text for the header
                        lotno.setTextColor(Color.BLACK); // set the color
                        lotno.setPadding(10, 10, 0, 10); // set the padding (if required)
                        lotno.setTextSize(14);
                                tr.addView(lotno);

                                final TextView itemname = new TextView(DashBoard.this);
                                itemname.setId(200+count);// define id that must be unique
                                itemname.setText(""+json_data.getString("Prep_MenuItem")); // set the text for the header
                                itemname.setTextColor(Color.BLACK); // set the color
                                itemname.setMaxLines(3);
                                itemname.setPadding(10, 10, 0, 10); // set the padding (if required)
                                itemname.setTextSize(14);
                                tr.addView(itemname);

                                TextView qty = new TextView(DashBoard.this);
                                qty.setId(200+count);// define id that must be unique
                                qty.setText(""+json_data.getString("Prep_Quantity")); // set the text for the header
                                qty.setTextColor(Color.BLACK); // set the color
                                qty.setPadding(50, 10, 0, 10); // set the padding (if required)
                                qty.setTextSize(14);
                                tr.addView(qty);

                                TextView order = new TextView(DashBoard.this);
                                order.setId(200+count);// define id that must be unique
                                order.setText(""+json_data.getString("Prep_Order_Number")); // set the text for the header
                                order.setTextColor(Color.BLACK); // set the color
                                order.setPadding(40, 10, 0, 10); // set the padding (if required)
                                order.setTextSize(14);
                                tr.addView(order);

                                TextView notes = new TextView(DashBoard.this);
                                notes.setId(200+count);// define id that must be unique
                                notes.setText(""+json_data.getString("Prep_Notes")); // set the text for the header
                                notes.setTextColor(Color.BLACK); // set the color
                                notes.setPadding(40, 10, 0, 10); // set the padding (if required)
                                notes.setTextSize(14);
                                tr.addView(notes);

                            final Button start = new Button(DashBoard.this);
                            start.setText("Start");
                            start.setMaxWidth(10);
                            start.setTextSize(16);
                            start.setTypeface(null, Typeface.BOLD);
                            start.setBackgroundColor(Color.RED);
                        tr.addView(start);

                        final Button complete = new Button(DashBoard.this);
                        complete.setText("Complete");
                        complete.setMaxWidth(10);
                        complete.setTextSize(16);
                        complete.setTypeface(null, Typeface.BOLD);
                        complete.setBackgroundColor(Color.BLUE);
                        tr.addView(complete);

                        final Button delivered = new Button(DashBoard.this);
                        delivered.setText("Delivered");
                        delivered.setMaxWidth(10);
                        delivered.setTextSize(16);
                        delivered.setTypeface(null, Typeface.BOLD);
                        delivered.setBackgroundColor(Color.BLUE);
                        tr.addView(delivered);

                                tl.addView(tr, new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));

               count++;

}

person Goofy    schedule 22.11.2012    source источник
comment
Попробуйте это.. android:shrinkColumns=7 android:stretchColumns=1. Вместо (*) вы написали и, пожалуйста, сообщите мне результат   -  person Narendra Pal    schedule 22.11.2012
comment
а затем вы можете указать отступы или поля, что хотите   -  person Narendra Pal    schedule 22.11.2012
comment
@ник, я должен сделать это программно...   -  person Goofy    schedule 22.11.2012
comment
Я думаю, что после добавления представления на tl вы должны установить маржу   -  person Narendra Pal    schedule 22.11.2012
comment
tl.addView(tr, новый TableLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); После этой строки вы должны установить поле. В моем случае, чтобы нарисовать разделитель, я использовал этот шаг.   -  person Narendra Pal    schedule 22.11.2012
comment
stackoverflow.com/a/5258310/1395259. Пожалуйста, прочитайте эту ссылку   -  person Narendra Pal    schedule 22.11.2012
comment
Глупо, почему вы не хотите использовать xml. Любой резонанс   -  person Narendra Pal    schedule 22.11.2012
comment
@ник как это? могу реализовать...   -  person Goofy    schedule 22.11.2012
comment
См. основной фундамент - это право на разделение. Так что просто нарисуйте ImageView между двумя кнопками, как это. ‹ImageView android:layout_width=2dp android:layout_height=30dp android:background=@color/black /›   -  person Narendra Pal    schedule 22.11.2012
comment
@nick я программно добавляю кнопки ...   -  person Goofy    schedule 22.11.2012
comment
Таким образом, вы можете добавить ImageView также программно.   -  person Narendra Pal    schedule 22.11.2012
comment
давайте продолжим обсуждение в чате   -  person Goofy    schedule 22.11.2012


Ответы (2)


добавьте свои кнопки, используя следующий код, и если вы хотите увеличить пространство, увеличьте значение leftMargin с 10 до 15 или 20.

            final Button start = new Button(DashBoard.this);
            ViewGroup.MarginLayoutParams lpButton = (ViewGroup.MarginLayoutParams) start.getLayoutParams();
            lpButton.setMargins(10, 0, 0, 0);
                start.setText("Start");
                start.setMaxWidth(10);
                start.setTextSize(16);
                start.setTypeface(null, Typeface.BOLD);
                start.setBackgroundColor(Color.RED);
                start.setLayoutParams(lpButton);
                tr.addView(start);

            final Button complete = new Button(DashBoard.this);
            complete.setText("Complete");
            complete.setMaxWidth(10);
            complete.setTextSize(16);
            complete.setTypeface(null, Typeface.BOLD);
            complete.setBackgroundColor(Color.BLUE);
            complete.setLayoutParams(lpButton);
            tr.addView(complete);

            final Button delivered = new Button(DashBoard.this);
            delivered.setText("Delivered");
            delivered.setMaxWidth(10);
            delivered.setTextSize(16);
            delivered.setTypeface(null, Typeface.BOLD);
            delivered.setBackgroundColor(Color.BLUE);
            delivered.setLayoutParams(lpButton);
            tr.addView(delivered);
person Mehul Joisar    schedule 22.11.2012

попробуйте дать paddingLeft или paddingRight кнопкам. Кроме того, вы можете указать отступы для макета, который их обертывает.

person ziniestro    schedule 22.11.2012
comment
Вы можете добавить отступы для каждого отдельного представления в TableRow вашего кода, выполнив следующие действия: 20, 20, 20); } ‹/пред› - person ziniestro; 22.11.2012