2013-12-17 34 views
0

我正在開發一個android項目,我正在使用表格佈局,但遇到了一個問題。在TableLayout上拉伸列沒有效果

正如標題中提到的,我使用TableLayout並試圖拉伸所有列以適應屏幕,但由於某種原因它沒有任何影響,並且所有列均被擠壓到左側。

以下是XML佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scrollbars="horizontal"> 
     <HorizontalScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <TableLayout android:id="@+id/resultTable" 
       android:stretchColumns="*" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"> 
      </TableLayout> 
     </HorizontalScrollView> 
    </ScrollView> 
</LinearLayout> 

下面是我如何填充TableLayout

getActivity().runOnUiThread(new Runnable() { 

       @Override 
       public void run() { 
        try 
        { 
         Log.d("Returned in Result", String.valueOf(result.length())); 
         resultTable.removeAllViews(); 
         resultTable.setStretchAllColumns(true); 
         for (int i = 0; i < result.length(); i++) 
         { 
          TableRow tr = new TableRow(getActivity()); 
          JSONArray array = result.getJSONArray(i); 
          Log.d("Returned in Result", "Loaded Columns: " + String.valueOf(array.length())); 
          for (int j = 0; j < array.length(); j++) 
          { 
           //LinearLayout linearLayout = new LinearLayout(getActivity()); 
           //LayoutParams parms = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
           //linearLayout.setLayoutParams(parms); 

           TextView textView = new TextView(getActivity()); 
           textView.setText(array.getString(j)); 
           textView.setSingleLine(); 
           //linearLayout.addView(textView); 
           tr.addView(textView); 
          } 
          resultTable.addView(tr); 
         } 
        } 
        catch (Exception ex) 
        { 
         Log.e("Load Result", ex.toString()); 
         CrashReporter.ReportCrash(ex, Severity.Critical); 
        } 
       } 
      }); 

感謝您的幫助,您可以提供。

UPDATE

我發現它是導致該列不會延長HorizontalScrollView。如果我忽略水平滾動視圖,它可以正常工作,但在水平滾動視圖中,列不會伸展。我怎樣才能得到這個工作,因爲我需要水平滾動視圖。

回答

0

我可能是錯的,但我有類似的東西,我沒有過任何表格佈局,但我希望有一個相對佈局內滾動視圖,所以我不得不這樣做,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/theme" 
android:orientation="vertical" > 

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="34dp" > 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 
    </LinearLayout> 
</ScrollView> 

這非常不起作用,但這是我的「列向左擠壓」。問題解決了。 編輯:嘗試讓您的線性佈局內滾動視圖。

+0

謝謝但不幸運 – Boardy