2016-11-13 52 views
0

我正在嘗試創建一個新的表格行並將其添加到現有的TableLayout中。我搜索了互聯網,似乎我已經嘗試了一切,但我無法獲得新的行顯示。如果我在佈局中添加一個帶有ID的TableRow,並將我的自定義ImageView添加到該佈局,而無需將新的TableRow添加到佈局,則ImageView會顯示,但我需要動態添加一個新行。在我的片段:如何動態添加TableRows?

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_default_view, 
    container, false); 

    final ViewGroup tableLayout = (ViewGroup) rootView.findViewById 
    (R.id.table_bracket); 

    TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(
      TableRow.LayoutParams.MATCH_PARENT, 
      TableRow.LayoutParams.MATCH_PARENT 
); 

    TableRow tableRow = new TableRow(context); 
    tableRow.setLayoutParams(layoutParams); 

    SeparatorImageView separator = new SeparatorImageView(getActivity()); 
    separator.setLayoutParams(layoutParams); 

    tableRow.addView(separator); 
    tableLayout.addView(tableRow); 
    return rootView; 
} 

這裏是我想要添加到佈局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.mbusby.bracketsandbox.DefaultViewFragment"> 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/table_bracket" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="*"> 

</TableLayout> 

回答

0

添加tableLayout.addView(tablerow的)後,這條線;線

getWindow().getDecorView().findViewById(android.R.id.content).invalidate(); 

刷新tableview中新添加的行會看到

+0

似乎並沒有工作。我正在使用一個片段,所以我寫了相同的東西,但與之前的getActivity() – Medivh

+0

顯示你的代碼你寫了什麼來驗證 – siddhesh

+0

我直接在tableLayout.getView(tableRow):getActivity()。getWindow()後添加了這個。 。getDecorView()findViewById(android.R.id.content).invalidate(); – Medivh