Android table 整齐对齐冻结 header
Android table align tidy with frozen header
我正在尝试用冻结的 header 创建一个 table。我可以通过一个 table 布局来实现这一点,其中包含一个滚动视图包装另一个 table 布局,但我的 header 对齐不正确。
这是XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/table_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
</TableLayout>
</ScrollView>
</TableLayout>
这是代码
public class PrepositionRulesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rules);
init();
}
public void init(){
createTableHeader();
createTableBody();
// alignRows();
}
private Collection<Preposition> load(Context context) {
DataSource<Preposition> dataSource = new DataSourceFactory().createPrepositionDataSource(context);
return dataSource.get();
}
private void createTableHeader() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_header);
tableLayout.addView(createHeaderRow(), 0);
}
private void createTableBody() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_body);
int row = 0;
TableRow tableRow = createHeaderRow();
tableLayout.addView(tableRow, row++);
for (Preposition preposition : load(this)) {
tableLayout.addView(createBodyRow(preposition), row++);
}
}
private TableRow createHeaderRow() {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(getString(R.string.preposition)));
row.addView(createCell(Kasus.Akkusativ.name()));
row.addView(createCell(Kasus.Dativ.name()));
row.addView(createCell(Kasus.Genitiv.name()));
return row;
}
private TableRow createBodyRow(Preposition preposition) {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(preposition.getPreposition()));
row.addView(createCell(preposition.contains(Kasus.Akkusativ)));
row.addView(createCell(preposition.contains(Kasus.Dativ)));
row.addView(createCell(preposition.contains(Kasus.Genitiv)));
return row;
}
private void alignRows() {
TableLayout headerLayout = (TableLayout) findViewById(R.id.table_header);
TableRow headerRow = (TableRow)headerLayout.getChildAt(0);
TableLayout bodyLayout = (TableLayout) findViewById(R.id.table_body);
TableRow bodyRow = (TableRow)bodyLayout.getChildAt(0);
headerLayout.setLayoutParams(bodyLayout.getLayoutParams());
for(int i = 0; i < bodyRow.getChildCount(); i++) {
headerRow.getChildAt(i).setLayoutParams(bodyRow.getChildAt(i).getLayoutParams());
}
}
private View createCell(boolean isEnabled) {
if (isEnabled) {
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.ic_done_black_24dp);
return view;
}
return new Space(this);
}
private View createCell(String text) {
TextView view = new TextView(this);
view.setPadding(15, 0, 15, 5);
view.setTypeface(Typeface.DEFAULT_BOLD);
view.setText(text);
return view;
}
}
我认为我可以通过将 header 行的副本添加到 body table 而不是将其布局复制到添加到 [=33 的行来实现对齐=] table 但这没有用。
如有任何建议,我们将不胜感激
图像显示错位:
很难控制 table 布局中的对齐方式,因为一列的宽度始终取决于同一 table 中另一行的另一列的宽度,并且会受到影响。 =13=]
为了创建 table 整齐对齐,我建议您使用 LinearLayout(用于 header 和行)和列表视图来显示项目(行)。
示例 row.xml table 的 header 和列表的行:
<LinearLayout
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="beginning|middle|end">
<TextView
android:id="@+id/model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:padding="4dp"
android:text="@string/model"
android:textStyle="bold"/>
<TextView
android:id="@+id/color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/color"
android:textStyle="bold"/>
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="@string/status"
android:textStyle="bold"/>
</LinearLayout>
这就是您在 activity_main.xml 中应用它的方式:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<include layout="@layout/row"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
最后,您为列表视图适配器中的每个列表项膨胀相同的 row.xml。下面是上述 xmls 创建的 table 的屏幕截图。请注意,在 table.
中显示每个行项目时,我已删除分隔线并以编程方式将文本样式设置为正常
我正在尝试用冻结的 header 创建一个 table。我可以通过一个 table 布局来实现这一点,其中包含一个滚动视图包装另一个 table 布局,但我的 header 对齐不正确。
这是XML
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/table_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
</TableLayout>
</ScrollView>
</TableLayout>
这是代码
public class PrepositionRulesActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rules);
init();
}
public void init(){
createTableHeader();
createTableBody();
// alignRows();
}
private Collection<Preposition> load(Context context) {
DataSource<Preposition> dataSource = new DataSourceFactory().createPrepositionDataSource(context);
return dataSource.get();
}
private void createTableHeader() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_header);
tableLayout.addView(createHeaderRow(), 0);
}
private void createTableBody() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.table_body);
int row = 0;
TableRow tableRow = createHeaderRow();
tableLayout.addView(tableRow, row++);
for (Preposition preposition : load(this)) {
tableLayout.addView(createBodyRow(preposition), row++);
}
}
private TableRow createHeaderRow() {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(getString(R.string.preposition)));
row.addView(createCell(Kasus.Akkusativ.name()));
row.addView(createCell(Kasus.Dativ.name()));
row.addView(createCell(Kasus.Genitiv.name()));
return row;
}
private TableRow createBodyRow(Preposition preposition) {
TableRow row = new TableRow(this);
TableRow.LayoutParams rowLayout = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rowLayout.gravity = Gravity.CENTER_HORIZONTAL;
row.setLayoutParams(rowLayout);
row.addView(createCell(preposition.getPreposition()));
row.addView(createCell(preposition.contains(Kasus.Akkusativ)));
row.addView(createCell(preposition.contains(Kasus.Dativ)));
row.addView(createCell(preposition.contains(Kasus.Genitiv)));
return row;
}
private void alignRows() {
TableLayout headerLayout = (TableLayout) findViewById(R.id.table_header);
TableRow headerRow = (TableRow)headerLayout.getChildAt(0);
TableLayout bodyLayout = (TableLayout) findViewById(R.id.table_body);
TableRow bodyRow = (TableRow)bodyLayout.getChildAt(0);
headerLayout.setLayoutParams(bodyLayout.getLayoutParams());
for(int i = 0; i < bodyRow.getChildCount(); i++) {
headerRow.getChildAt(i).setLayoutParams(bodyRow.getChildAt(i).getLayoutParams());
}
}
private View createCell(boolean isEnabled) {
if (isEnabled) {
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.ic_done_black_24dp);
return view;
}
return new Space(this);
}
private View createCell(String text) {
TextView view = new TextView(this);
view.setPadding(15, 0, 15, 5);
view.setTypeface(Typeface.DEFAULT_BOLD);
view.setText(text);
return view;
}
}
我认为我可以通过将 header 行的副本添加到 body table 而不是将其布局复制到添加到 [=33 的行来实现对齐=] table 但这没有用。
如有任何建议,我们将不胜感激
图像显示错位:
很难控制 table 布局中的对齐方式,因为一列的宽度始终取决于同一 table 中另一行的另一列的宽度,并且会受到影响。 =13=]
为了创建 table 整齐对齐,我建议您使用 LinearLayout(用于 header 和行)和列表视图来显示项目(行)。
示例 row.xml table 的 header 和列表的行:
<LinearLayout
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="beginning|middle|end">
<TextView
android:id="@+id/model"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:padding="4dp"
android:text="@string/model"
android:textStyle="bold"/>
<TextView
android:id="@+id/color"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="4dp"
android:text="@string/color"
android:textStyle="bold"/>
<TextView
android:id="@+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="@string/status"
android:textStyle="bold"/>
</LinearLayout>
这就是您在 activity_main.xml 中应用它的方式:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<include layout="@layout/row"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/dividerHorizontal"/>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
最后,您为列表视图适配器中的每个列表项膨胀相同的 row.xml。下面是上述 xmls 创建的 table 的屏幕截图。请注意,在 table.
中显示每个行项目时,我已删除分隔线并以编程方式将文本样式设置为正常