2015-10-05 37 views
0

我試圖動態創建視圖以充當表格行之間的間隔符,但是我的視圖只與文本一樣寬,即使我試圖讓它們與父項匹配。以編程方式添加視圖以匹配父級以填充TableLayout

enter image description here

我動態地添加到TableLayout與

TableRow tr = new TableRow(this);  

tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
TableRow.LayoutParams.WRAP_CONTENT)); 

LinearLayout layout = new LinearLayout(this); 
layout.setOrientation(LinearLayout.VERTICAL); 
tr.setOrientation(LinearLayout.VERTICAL); 
tr.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 

layout.setPadding(15, 15, 10, 15); 

TextView description = new TextView(this); 
description.setText(row.getString(TAG_DESCRIPTION)); 
description.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black)); 
description.setTextSize(25); 

TextView dateCreated = new TextView(this); 
dateCreated.setText("Date Created: " + row.getString(TAG_DATEADDED).replace("\\", "")); 
dateCreated.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black)); 
dateCreated.setTextSize(20); 

TextView reportType = new TextView(this); 
reportType.setText("Report Type: " + row.getString(TAG_REPTYPE)); 
reportType.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.black)); 
reportType.setTextSize(20); 

View view = new View(this); 
view.setMinimumHeight(2); 
view.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.black)); 
view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT)); 

layout.addView(description); 
layout.addView(dateCreated); 
layout.addView(reportType); 
layout.addView(view); 

tr.addView(layout); 

existingReports.addView(tr); 

,我定義我TableLayout在我的XML與

<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/txt_numExistingReports" 
    android:layout_alignStart="@+id/txt_numExistingReports" 
    android:layout_below="@+id/txt_numExistingReports" 
    android:layout_alignParentBottom="true" 
    android:background="@android:color/white" 
    android:layout_toLeftOf="@+id/home" 
    android:layout_toStartOf="@+id/home"> 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="30dp" 
     android:showDividers="middle" 
     android:id="@+id/existingReports" 
     android:layout_marginTop="5dp" 
     android:divider="?android:attr/dividerHorizontal" 
     android:background="@android:color/white"/> 
</ScrollView> 

回答

0

滾動視圖寬度裹內容...嘗試與匹配父

+0

不幸的是沒有任何影響 –

+0

嗯,奇怪......我會離開辦公室sryyyy回到你身邊 –

相關問題