2011-01-27 46 views
6

Trong cách bố trí bàn tôi có một tablerow và trong tablerow mà tôi có 6 hộp chỉnh sửa văn bản và tôi muốn thiết lập lề bố trí cho rằng 6 hộp chỉnh sửa văn bảnLàm cách nào để đặt lề bố cục của hộp văn bản chỉnh sửa?

TableLayout t1=(TableLayout)findViewById(R.id.table_layout01); 

    TableRow tr1=new TableRow(inventory.this); 
    tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 


    tr1.setBackgroundColor(Color.BLACK); 
    EditText ed6=new EditText(inventory.this); 
    //ed6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    /*ViewGroup.MarginLayoutParams editmargin=new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.FILL_PARENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT); 
    editmargin.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);*/ 


    ed6.setTextColor(Color.BLACK); 
    ed6.setBackgroundColor(Color.WHITE); 


    ed6.setText("1"); 
     tr1.addView(ed6); 



    EditText ed7=new EditText(inventory.this); 
    //ed7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    ed7.setTextColor(Color.BLACK); 
    ed7.setBackgroundColor(Color.WHITE); 
    ed7.setText("2"); 

    tr1.addView(ed7); 

    EditText ed8=new EditText(inventory.this); 
    //ed8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    ed8.setTextColor(Color.BLACK); 
    ed8.setBackgroundColor(Color.WHITE); 
    ed8.setText("3"); 

    tr1.addView(ed8); 

    EditText ed9=new EditText(inventory.this); 
    //ed9.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    ed9.setTextColor(Color.BLACK); 
    ed9.setBackgroundColor(Color.WHITE); 
    ed9.setText("4"); 

    tr1.addView(ed9); 

    EditText ed10=new EditText(inventory.this); 
    //ed10.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    ed10.setTextColor(Color.BLACK); 
    ed10.setText("5"); 
    ed10.setBackgroundColor(Color.WHITE); 

    tr1.addView(ed10); 

    EditText ed11=new EditText(inventory.this); 
    //ed11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); 
    ed11.setTextColor(Color.BLACK); 
    ed11.setText("6"); 
    ed11.setBackgroundColor(Color.WHITE); 

    tr1.addView(ed11); 

    t1.addView(tr1); 

Trả lời

4

EDIT:
tôi sẽ cố gắng với XML bên dưới (bạn dĩ nhiên, cập nhật id của v.v.). "Ma thuật" trong xml là nó phân phối tất cả chiều rộng có sẵn đồng đều trong số TextView 's (và EditText' s trên hàng thứ hai).

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

    <!-- The first "row" -->  
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="TextView 01" 
      android:id="@+id/textView01" /> 

     <!-- Here you'd add your other five TextView's accordingly --> 

    </LinearLayout> 

    <!-- The second "row" -->  
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 

     <EditText 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="TextView 01" 
      android:id="@+id/editText01" /> 

     <!-- Here you'd add your other five EditText's accordingly --> 

    </LinearLayout> 
</LinearLayout> 

Trong mã Java của bạn sau đó bạn có thể truy cập vào xem EditText của bạn như:

EditText editText01 = (EditText) findViewById(R.id.editText01); 
editText01.setText("1"); 

bây giờ tôi đã bỏ qua thực tế là bạn cần phải tạo ra EditText của bạn programatically. Bạn có thực sự, thực sự cần phải tạo chúng trong Java? (Tại sao?)

OLD ĐÁP:
Nếu bạn chỉ muốn thiết lập lề bố trí để xem EditText của bạn tôi quess bạn có thể sử dụng cuộc gọi setMargins(left, top, right, bottom) chức năng trên biến LayoutParams.

int left = 6; 
int top = 12; 
int right = 6; 
int bottom = 6; 

TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
params.setMargins(left, top, right, bottom); 

EditText edXY = new EditText(inventory.this); 
edXY.setLayoutParams(params); 

Nếu cuối cùng bạn muốn phân phối tất cả các không gian có sẵn đều trong số sáu lần xem EditText trong một dòng của bảng tôi sẽ đề nghị bạn có một cái nhìn tại bưu sau: 2-column TableLayout with 50% exactly for each column

+0

thnx cho trả lời thưa của bạn ... nhưng nó không làm việc – Arun

+0

OK. Các triệu chứng như thế nào? Hành vi mong đợi và thực tế là gì? – dbm

+0

thực sự tôi có một layout.in bảng mà tôi có hai hàng bảng. trong tablerow đầu tiên tôi có sáu cột và mỗi cột có mỗi lần xem văn bản. trong hàng thứ hai tôi có sáu hộp cedittext và tôi muốn hàng thứ hai đó giống như hàng đầu tiên có nghĩa là layout.i lề tương tự tạo ra hàng đầu tiên trong xml.but không thể tạo hàng thứ hai trong xml bcoz tôi phải làm điều đó theo chương trình ... – Arun

8

trước hết là một cái gì đó bạn nên biết : Theo số Official Android Dev Pages, Chế độ xem (và TextView có nguồn gốc từ Chế độ xem) không hỗ trợ cài đặt Ký quỹ, nhưng Chế độ xem nhóm (chẳng hạn như LinearLayout, RelativeLayout v.v ...).

Vì vậy, những gì bạn có thể làm như sau:

TableLayout.LayoutParams params = new TableLayout.LayoutParams(); 
params.setMargins(5, 5, 5, 5); 
TextView view = new TextView(this); 
view.setLayoutParams(params); 

này sẽ thiết lập lề cho tất cả trẻ em 5 pixel - Tôi đã thử nó và nó làm việc cho tôi (dù với một LinearLayout với sự liên kết dọc). Cung cấp cho nó một shot và cho tôi biết nếu tôi có thể giúp thêm :).

Chúc mừng,

Ready4Fajir

Các vấn đề liên quan