简体   繁体   中英

How to set margin of textview programatically within tablerow

Hy.... I'm making a project on eclipse. In this project I made an xml layout like this :

 <ScrollView 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"    
        tools:context=".AddRemoveMainActivity" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/main_layout"
        >

        <!-- I will add some view programatically in this line -->

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

        <Button 
            android:id="@+id/b_add"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Add"
             android:onClick="addItems"
            /> 
            <Button 
            android:id="@+id/b_del"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="Del"
             android:onClick="deleteItems"
            />     
        </TableRow>
    </LinearLayout>    
    </ScrollView>

And on my mainactivity :

public class AddRemoveMainActivity extends Activity {
    Button add,del;
    LinearLayout ll;
    private String[] spinnerContent = {"Ikan","Udang","Kepiting","Kerang"};

    private int count = 0;
    private int line = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_remove_main);

        ll = (LinearLayout) findViewById(R.id.main_layout); 

    }

    public void addItems(View view)
    {
        count++;

        final LinearLayout frame = new LinearLayout(this);
        frame.setOrientation(LinearLayout.VERTICAL);
        frame.setId(count);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinnerContent);

        final TableRow row1 = new TableRow(this);           

        Spinner spin = new Spinner(this);
        TextView jenis = new TextView(this);
        TextView keterangan = new TextView(this);
        TextView total = new TextView(this);


        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        params.setMargins(0, 13, 17, 9);


        spin.setAdapter(adapter);
        jenis.setText("Jenis : "+Integer.toString(count));
        //jenis.setLayoutParams(params);
        keterangan.setText("Keterangan : ");
        total.setText("Jumlah(kg) :");

        row1.addView(jenis);
        row1.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        //ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
        //mlp.setMargins(37, 13, 17, 0);


        row1.addView(spin);

        frame.addView(row1);

        ll.addView(frame, line);
        line++;

    }


    public void deleteItems(View view)
    {
        if(count > 0)
        {
            final LinearLayout temp = (LinearLayout)ll.findViewById(count);
            temp.removeAllViews();
            ll.removeView(temp);
            count--;
            line--;
        }
    }

As you can see from my mainActivity, I have added textview(jenis) and spinner(spin) to a tableRow, then I added that tableRow to a linearLayout(frame) then added that frame to the linearLayout that came from in xml file above the two buttons (Add & Del button)....... My problem here is that I just want to add a margin between my textview (jenis) and (Spinner) spin. As you can see again from my mainActivity, I have tried using :

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            params.setMargins(0, 13, 17, 9);
...
...
...
jenis.setLayoutParams(params);

But this one is not work... And I have tried another way but it's not work too :

//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
            //mlp.setMargins(37, 13, 17, 0);

So Any idea or another way for adding margin to my textview...??? Thanks in advance... :-)

Try the below code and let me know if it works

android.widget.TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
    layoutParams.setMargins(0, 13, 17, 9);
    row1.addView(jenis, layoutParams);
    //ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
    //mlp.setMargins(37, 13, 17, 0);


    row1.addView(spin, layoutParams);

That works:

public static void setRunTimeMargin(View v, int left, int top, int right,
                                    int bottom) {

    ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
            .getLayoutParams();

    mlp.setMargins(left, top, right, bottom);
// NJ forgot the last important step, set LayoutParams on the TextView:
    v.setLayoutParams(mlp);

}

Please add this method and check . it worked for me

public static void setRunTimeMargin(View v, int left, int top, int right,
                                        int bottom) {

        ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) v
                .getLayoutParams();

        mlp.setMargins(left, top, right, bottom);
    }

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