2016-03-07 139 views
0

我正在开发一个android项目,我有一个表格布局。我在表格标题的XML布局文件中有一行表格布局。表格的其余部分是根据API的结果动态填充的。将视图添加到表格布局的底部

我将视图添加到表格布局,但我动态添加的行出现在表格标题上方。

下面是我的XML布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <include layout="@layout/toolbar" /> 
    <TableLayout android:id="@+id/tblAuthenticatedDevices" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1" 
     android:shrinkColumns="false" 
     android:layout_margin="10dp"/> 
     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/cardToolbar"> 
      <TextView 
       android:text="Device Name" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:textStyle="bold" 
       android:padding="10dp" 
       android:textColor="@color/white" /> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:text="Last Logged In" 
       android:textStyle="bold" 
       android:padding="10dp" 
       android:textColor="@color/white" /> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:text="From" 
       android:textStyle="bold" 
       android:padding="10dp" 
       android:textColor="@color/white"/> 
     </TableRow> 
</LinearLayout> 

下面是我怎么了填充表的其余部分

JSONArray dataObject = jsonObject.getJSONArray(Defines.JSONElements.DATA); 
      for (int i = 0; i < dataObject.length(); i++) 
      { 
       TableRow row = (TableRow)getLayoutInflater().inflate(R.layout.table_row_layout, tblAuthenticatedDevices, false); 
       JSONObject deviceData = dataObject.getJSONObject(i); 

       TextView txtDeviceName = new TextView(AuthenticatedDevices.this); 
       txtDeviceName.setText(deviceData.getString("DeviceName")); 
       row.addView(txtDeviceName); 

       TextView txtLastLoggedIn = new TextView(AuthenticatedDevices.this); 
       txtLastLoggedIn.setText(deviceData.getString("LastLoggedIn")); 
       row.addView(txtLastLoggedIn); 

       TextView txtLocation = new TextView(AuthenticatedDevices.this); 
       txtLocation.setText(String.format("%s - %s", deviceData.getString("Country"), deviceData.getString("City"))); 
       row.addView(txtLocation); 
       tblAuthenticatedDevices.addView(row); 
      } 

只是再次重申,该行数据,我动态地添加到该表格出现在我添加到我的XML布局的行标题上方。我希望行标题出现,然后在下面显示我添加到表中的行。

感谢您提供的任何帮助。

回答

0

我已经意识到我做了什么错了,当我又看看今天早上。

我已经结束了表格布局(即我有<TableLayout... />),然后我添加了我的TableRow,但它在TableLayout之外。

取而代之,我已将标题放在TableLayout标签内,如下所示,并且按预期工作。

<TableLayout android:id="@+id/tblAuthenticatedDevices" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:stretchColumns="1" 
     android:shrinkColumns="false" 
     android:layout_margin="10dp"> 
     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/cardToolbar"> 
      <TextView 
       android:text="Device Name" 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:textStyle="bold" 
       android:padding="10dp" 
       android:textColor="@color/white" /> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:text="Last Logged In" 
       android:textStyle="bold" 
       android:padding="10dp" 
       android:textColor="@color/white" /> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:text="From" 
       android:textStyle="bold" 
       android:padding="10dp" 
       android:textColor="@color/white"/> 
     </TableRow> 
    </TableLayout> 
0

添加这个你创建自己的数据行创建头前:

 TableRow rowHeader = new TableRow(context); 
      rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0")); 
      rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, 
       TableLayout.LayoutParams.WRAP_CONTENT)); 

      //Title header for each column here 
      String[] headerText={"ID","NAME","TYPE"}; 

      for(String c:headerText) { 
      TextView tv = new TextView(this); 
      tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, 
        TableRow.LayoutParams.WRAP_CONTENT)); 
      tv.setGravity(Gravity.CENTER); 
      tv.setTextSize(18); 
      tv.setPadding(5, 5, 5, 5); 
      tv.setText(c); 
      rowHeader.addView(tv); 
      } 
      tableLayout.addView(rowHeader); 
2

好了,所以我一直在研究关于TableLayouts那里总是包括他们的头部,基本上,所有的人只是首先通过代码添加TableRow头部,以使其位于顶部。所以这就是我为你做的一个参考。首先,我为头部做了一个单独的布局。

tr_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:textStyle="bold" 
     android:id="@+id/tv_device_name" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="device name" /> 

    <TextView 
     android:id="@+id/tv_last_login" 
     android:textStyle="bold" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="last_login" /> 

    <TextView 
     android:id="@+id/tv_location" 
     android:layout_width="0dp" 
     android:textStyle="bold" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="location" /> 

</TableRow> 

然后在该tableLayout是活动的布局,我就一直是空的,是这样的...

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.aalawi.myapplication.MainActivity"> 

    <TableLayout 
     android:id="@+id/tblAuthenticatedDevices" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:shrinkColumns="false" 
     android:stretchColumns="1" /> 

</LinearLayout> 

然后在代码中,我只是先添加标题,然后在一些示例数据上迭代。另外,我认为你的table_rows也有不同的布局,就像你提供的代码中提到的那样(命名为table_row_layout),所以我只是做了类似的事情。

tl_row.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <TextView 
     android:id="@+id/tv_device_name" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/tv_last_login" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" /> 

    <TextView 
     android:id="@+id/tv_location" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" /> 

</TableRow> 

MainActivity.java

public class MainActivity extends AppCompatActivity { 


    private TableLayout tl; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     initViews(); 
    } 


    private void initViews() { 
     tl = (TableLayout) findViewById(R.id.tblAuthenticatedDevices); 

     List<String> deviceNames = Arrays.asList("1", "2", "3"); 
     List<String> lastLogin = Arrays.asList("11", "22", "33"); 
     List<String> location = Arrays.asList("111", "222", "333"); 

     // Add Header First... 
     TableRow header = (TableRow) getLayoutInflater().inflate(R.layout.tr_header, null); 
     tl.addView(header); 

     // Fill additional rows... 
     for (int i = 0; i < deviceNames.size(); i++) { 
      TableRow row = (TableRow) getLayoutInflater().inflate(R.layout.tl_row, tl, false); 
      TextView txtDeviceName = (TextView) row.findViewById(R.id.tv_device_name); 
      TextView txtLastLoggedIn = (TextView) row.findViewById(R.id.tv_last_login); 
      TextView txtLocation = (TextView) row.findViewById(R.id.tv_location); 

      txtDeviceName.setText(deviceNames.get(i)); 
      txtLastLoggedIn.setText(lastLogin.get(i)); 
      txtLocation.setText(location.get(i)); 

      tl.addView(row); 
     } 
    } 

} 

这导致了这一点。

enter image description here

相关问题