2015-05-09 95 views
0

Hy ....我正在做一個日食項目。在這個項目中我做了一個xml佈局是這樣的:如何以編程方式在表格內設置文本邊緣的邊緣

<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> 

而且我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--; 
     } 
    } 

正如你可以從我的mainActivity看,我已經加入的TextView(JENIS)和旋轉(旋轉)到一個tableRow,然後我將tableRow添加到linearLayout(框架),然後將該框架添加到來自xml文件上方的兩個按鈕(添加& Del按鈕)的linearLayout ....... 我的問題在這裏是我只是想在我的textview(jenis)和(Spinner)旋轉之間添加一個邊距。正如你可以從我的mainActivity再見到,我已經嘗試使用:

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

但是這一次是不行的......我都試過另一種方式,但它沒有工作過:

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

所以任何想法或其他方式爲我的textview增加保證金... ??? 在此先感謝... :-)

+0

我已經把我所有的源代碼,在此:https://github.com/HyosokaPoipo/Add_Remove_View –

回答

2

嘗試下面的代碼,讓我知道,如果它

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); 
+0

這個爲我工作得很好..謝謝多拉... :-) 我剛剛意識到我使用LinearLayout.LayoutParams ...所以只需要將其更改爲TableRow.LayoutParams .... ;-) Thankssss .... –

+0

@HyosokaPoipo歡迎:) – dora

0

請添加此方法並檢查。它爲我工作

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); 
    } 
+0

當使用mlp.setMargin(左,上,右,下)時有一個例外... eclipse說它是空指針異常....但是,謝謝你的回答mr.Nilesh ... :-) –