2013-05-15 3 views
1

프로그래밍 방식으로 TableLayout (id = myProducts_tableLayout)을 통해 테이블을 만들었지 만 화면에 표시되지 않습니다. 아래는 XML 코드입니다.테이블이 표시되지 않습니다

#`<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true"> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:background="#ffffff"> 

     <!-- Header Starts--> 
     <LinearLayout android:id="@+id/header" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@layout/header_gradient" 
       android:paddingTop="5dip" 
       android:paddingBottom="5dip"> 
       <!-- Logo Start--> 
       <ImageView android:src="@drawable/real_time_bidding" 
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_marginLeft="10dip"/> 
       <!-- Logo Ends --> 
     </LinearLayout> 
     <!-- Header Ends --> 

     <!-- Footer Start --> 
     <LinearLayout android:id="@+id/footer" 
       android:layout_width="fill_parent" 
       android:layout_height="90dip" 
       android:background="@layout/footer" 
       android:layout_alignParentBottom="true"> 
     </LinearLayout> 
     <!-- Footer Ends --> 

     <!-- All Products View --> 
     <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="10dip" 
       android:layout_below="@id/header"> 

       <TextView android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textColor="#153E7E" 
       android:textSize="25px" 
       android:textStyle="bold" 
       android:typeface="serif" 
       android:gravity="center" 
       android:text="My Products"/> 

      <TableLayout 
       android:id="@+id/myProducts_tableLayout" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:stretchColumns="0" 
       > 
      </TableLayout> 

      <!-- Link to Enter New Product -->  
      <TextView android:id="@+id/link_to_allProducts" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="213dip" 
       android:layout_marginBottom="50dip" 
       android:textColor="#153E7E" 
       android:textSize="20px" 
       android:textStyle="bold|italic" 
       android:typeface="serif" 
       android:gravity="center" 
       android:text="Back to All Products"/> 
     </LinearLayout> 
     <!-- All Products View Ends --> 

    </RelativeLayout> 
</ScrollView> 
` 

은 다음 중 어느 한 오류를 지적 시겠어요 된 .java 코드

super.onCreate(savedInstanceState); 
setContentView(R.layout.my_products);  

String[] sampleTxt = {"txt1", "txt2", "txt3"}; 
ScrollView sv = new ScrollView(this); 
final TableLayout layout = TableLayout findViewById(R.id.myProducts_tableLayout); 
layout.setLayoutParams(new TableLayout.LayoutParams(4, 5)); 

for(int i = 0; i < sources.length; i++) 
{ 
    final TableRow tr = new TableRow(this); 
    final TextView t = new TextView(this); 
    final Button b = new Button(this); 

    final LayoutParams textparams = new LayoutParams(400,40); // Width , height 
    t.setLayoutParams(textparams); 
    t.setTextSize(TypedValue.COMPLEX_UNIT_SP,10); 
    t.setTextColor(Color.BLUE); 

    final LayoutParams btnparams = new LayoutParams(20,20); 
    btnparams.setMargins(10, 20, 15, 0); // (left, top, right, bottom) 
    b.setLayoutParams(btnparams); 
    b.setBackgroundResource(R.drawable.delete); 


    t.setText(sampleTxt[i]); 

    b.setOnClickListener(new OnClickListener() 
    { 
     public void onClick(View v) 
     { 
      layout.removeView(tr); 
     } 
    }); 

    tr.addView(b); 
    tr.addView(t); 
    layout.addView(tr); 
} 

입니다. 그리고 또한 왜 일식이 TableLayout, TableRow, TextView, Button을 "final"로 만들지를 모르겠다. 그렇지 않으면 나에게 오류가 발생했다.

감사합니다.

답변

0

문제는 복사하지 않은 코드에 있습니다. 당신은 아마이 :

import android.view.ViewGroup.LayoutParams; 

하지만 당신이 원하는 :

import android.widget.TableRow.LayoutParams; 

내가 생각했을 것이다 첫 번째는 너무 일을 한 것입니다 만 폭과 높이를 지정하고 있기 때문에,하지만 그렇지 않은 것 같다. TableRow.LayoutParams는 ViewGroup.LayoutParams의 하위 클래스입니다. b.setLayoutParams() 또는 t.setLayoutParams를 호출하면 부모 인 TableRow에 대한 LayoutParams를 인수로 전달해야합니다.

+0

답장을 보내 주셔서 감사합니다 :). 아래는 가져 오기 파일이지만 화면에는 여전히 나타나지 않습니다. 'import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; 가져 오기 android.util.TypedValue; import android.view.View; 가져 오기 android.view.View.OnClickListener; 가져 오기 android.widget.Button; 가져 오기 android.widget.TableLayout; 가져 오기 android.widget.TableRow; 가져 오기 android.widget.TextView; import android.widget.TableRow.LayoutParams; – user2387107

+0

'for' 루프에서'sources.length'를'sampleTxt.length'로 변경하려고 시도했을 수도 있습니다 ('sources'는 게시 한 코드에서 정의되지 않았습니다). – toto2

+0

또한'layout.setLayoutParams (new TableLayout.LayoutParams (4, 5));'행을 제거해야합니다. – toto2

관련 문제