gpt4 book ai didi

Android 布局 - 如何实现固定/卡住的标题和列

转载 作者:IT老高 更新时间:2023-10-28 23:17:54 26 4
gpt4 key购买 nike

我想创建一个包含大量列 (7-10) 的类似表格的 View ,而标题行始终可见(即使向下滚动),并且第一列在水平滚动时也始终可见。

试图在 Horizo​​ntalScrollView 中放置一个 ListView ,让我显示一个具有水平和垂直滚动但没有静态列/标题的列表。我试图避免在用户滚动时使用多个 View 并在它们之间进行同步。

稍后我将不得不控制 View 内的事件,例如行/列点击,因此应该使用带有自定义适配器的东西。

有什么想法吗?

最佳答案

我会选择由 TableRow 填充的 TableLayout

以下代码演示了如何实现。

package com.test;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;

public class TableLayoutTest extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_layout);

TableRow.LayoutParams wrapWrapTableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int[] fixedColumnWidths = new int[]{20, 20, 20, 20, 20};
int[] scrollableColumnWidths = new int[]{20, 20, 20, 30, 30};
int fixedRowHeight = 50;
int fixedHeaderHeight = 60;

TableRow row = new TableRow(this);
//header (fixed vertically)
TableLayout header = (TableLayout) findViewById(R.id.table_header);
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.YELLOW);
row.addView(makeTableRowWithText("col 1", fixedColumnWidths[0], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 2", fixedColumnWidths[1], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 3", fixedColumnWidths[2], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 4", fixedColumnWidths[3], fixedHeaderHeight));
row.addView(makeTableRowWithText("col 5", fixedColumnWidths[4], fixedHeaderHeight));
header.addView(row);
//header (fixed horizontally)
TableLayout fixedColumn = (TableLayout) findViewById(R.id.fixed_column);
//rest of the table (within a scroll view)
TableLayout scrollablePart = (TableLayout) findViewById(R.id.scrollable_part);
for(int i = 0; i < 10; i++) {
TextView fixedView = makeTableRowWithText("row number " + i, scrollableColumnWidths[0], fixedRowHeight);
fixedView.setBackgroundColor(Color.BLUE);
fixedColumn.addView(fixedView);
row = new TableRow(this);
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.WHITE);
row.addView(makeTableRowWithText("value 2", scrollableColumnWidths[1], fixedRowHeight));
row.addView(makeTableRowWithText("value 3", scrollableColumnWidths[2], fixedRowHeight));
row.addView(makeTableRowWithText("value 4", scrollableColumnWidths[3], fixedRowHeight));
row.addView(makeTableRowWithText("value 5", scrollableColumnWidths[4], fixedRowHeight));
scrollablePart.addView(row);
}

}


//util method
private TextView recyclableTextView;

public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth, int fixedHeightInPixels) {
int screenWidth = getResources().getDisplayMetrics().widthPixels;
recyclableTextView = new TextView(this);
recyclableTextView.setText(text);
recyclableTextView.setTextColor(Color.BLACK);
recyclableTextView.setTextSize(20);
recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth / 100);
recyclableTextView.setHeight(fixedHeightInPixels);
return recyclableTextView;
}

}

页眉是不垂直滚动的部分;这就是您需要在列上设置固定宽度的原因。从您不想滚动的第一列开始,您必须为此设置行的固定高度。

这是布局 XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:id="@+id/fillable_area">
<TableLayout
android:id="@+id/table_header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:id="@+id/fillable_area">
<TableLayout
android:id="@+id/fixed_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id="@+id/scrollable_part"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>

刚加载时的输出是这样的

enter image description here

当滚动到右侧和底部时就像这样

enter image description here

关于Android 布局 - 如何实现固定/卡住的标题和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7119231/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com