将视图添加到 Table 布局的底部
Adding view to bottom of Table Layout
我正在做一个 android 项目,我有一个 table 布局。我有 Table 布局,在 XML 布局文件中有一行用于 table 标题。 table 的其余部分是根据 API.
的结果动态填充的
我将视图添加到 table 布局,但我动态添加的行显示在 table 标题上方。
下面是我的 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>
下面是我如何填充 table
的其余部分
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);
}
至于 re-iterate,我动态添加到 table 的行数据出现在我添加到 XML 布局的行标题上方。我希望行标题出现,然后在其下方出现我添加到 table 的行。
在创建数据行之前添加此内容以创建 header:
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);
好的,所以我一直在研究 TableLayouts,其中总是包含一个 header,基本上,所有这些都只是先通过代码添加 TableRow header,以便让它在最上面。所以这就是我为您所做的参考。首先,我只是为 header.
做了一个单独的布局
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的布局中,我就把它留空了,像这样...
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>
然后在代码中,我先添加 header,然后在一些示例数据上进行迭代。此外,我假设您的 table_rows 也有不同的布局,如您提供的代码中所述(named 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);
}
}
}
结果就是这样。
今天早上再看的时候才意识到自己做错了什么。
我结束了 Table 布局(即我有 <TableLayout... />
),然后我添加了我的 Table 行,但它在 Table 布局之外。
相反,我将标题放在 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>
我正在做一个 android 项目,我有一个 table 布局。我有 Table 布局,在 XML 布局文件中有一行用于 table 标题。 table 的其余部分是根据 API.
的结果动态填充的我将视图添加到 table 布局,但我动态添加的行显示在 table 标题上方。
下面是我的 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>
下面是我如何填充 table
的其余部分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);
}
至于 re-iterate,我动态添加到 table 的行数据出现在我添加到 XML 布局的行标题上方。我希望行标题出现,然后在其下方出现我添加到 table 的行。
在创建数据行之前添加此内容以创建 header:
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);
好的,所以我一直在研究 TableLayouts,其中总是包含一个 header,基本上,所有这些都只是先通过代码添加 TableRow header,以便让它在最上面。所以这就是我为您所做的参考。首先,我只是为 header.
做了一个单独的布局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的布局中,我就把它留空了,像这样...
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>
然后在代码中,我先添加 header,然后在一些示例数据上进行迭代。此外,我假设您的 table_rows 也有不同的布局,如您提供的代码中所述(named 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);
}
}
}
结果就是这样。
今天早上再看的时候才意识到自己做错了什么。
我结束了 Table 布局(即我有 <TableLayout... />
),然后我添加了我的 Table 行,但它在 Table 布局之外。
相反,我将标题放在 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>