0

我動態地從我的api獲取的JSON數據創建表格行和文字視圖。目前數據得到正確顯示,但不知何故我的滾動視圖不起作用。它不滾動。我確實添加了一個ScrollView,但我想我在那裏做錯了什麼。Android滾動視圖不適用於動態內容

我的XML:

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

    <ScrollView android:id="@+id/scrollView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:fillViewport="true" 
     android:orientation="vertical" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 

     <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" > 

      <TableLayout 
       android:id="@+id/tlMarksTable" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

      </TableLayout> 

     </LinearLayout> 

    </ScrollView> 

</RelativeLayout> 

而且部分中,我產生看法是這樣的:

jsonArray = new JSONArray(result); 

TableRow tableRow = new TableRow(context); 
setContentView(tableRow); 
tableRow.setOrientation(LinearLayout.VERTICAL); 

for(int i= 0; i < jsonArray.length(); i++) { 
    JSONObject jsonobject = jsonArray.getJSONObject(i); 
    String id   = jsonobject.getString("id"); 
    String content = jsonobject.getString("content"); 

    TextView textView = new TextView(context); 
    textView.setText(content); 
    if (Build.VERSION.SDK_INT < 23) { 
     textView.setTextAppearance(getApplicationContext(), R.style.boldText); 
    } else { 
     textView.setTextAppearance(R.style.boldText); 
    } 
    textView.setBackgroundResource(R.color.highlightedTextViewColor); 
    textView.setPadding(10, 10, 10, 10); 
    textView.setTextSize(getResources().getDimension(R.dimen.joke_content_font_size)); 
    tableRow.addView(textView); 
} 
+0

爲什麼不只是使用gridview? – tyczj

+0

@tyczj對不起,我沒有太多的經驗,你能解釋一下你的意思嗎? – utdev

+0

https://developer.android.com/guide/topics/ui/layout/gridview.html – tyczj

回答

0

配置了這樣你內心的組件:

  <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <TableLayout 
       android:id="@+id/tlMarksTable" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

      </TableLayout> 

     </LinearLayout> 

通過外部RelativeLayout ViewGroup的做法不多

+0

嗨,謝謝你的回答,我刪除了外部的'RelativeLayout'並用你提供的替換了xml,但是我仍然不能滾動任何其他想法? – utdev

+0

是ScrollView您的頂級ViewGroup? – Pooya

+0

yes ScrollView是我的頂級ViewGroup – utdev

0

我在這裏看到的主要問題是,LinearLayout封裝你的TableLayout,有android:layout_height="match_parent"。這意味着ScrollView的內層孩子將具有與父級相同的高度,從而形成非滾動佈局。

你爲什麼不嘗試去除那個LinearLayout,並將TableLayout作爲唯一的孩子?請記住將該孩子的身高設置爲android:layout_height="wrap_content"

順便說一句,我建議使用ListView顯示大量的數據。

最後一件事:不需要在每個佈局聲明中聲明xml名稱空間(xmlns:android="http://schemas.android.com/apk/res/android")。只有在根元素處有一個聲明才行;)

0

爲什麼不使用recyclerview? 你應該使用改造消耗WS

+0

你對WS有什麼意思? – utdev

+0

Web服務,來自API的數據 – Woz