2009-11-09 12 views
11

자, 이제이 TableLayout과 그 전체 데이터를 프로그래밍 방식으로 추가했습니다. HorizontalScrollView 내부에 TableLayout이 있는데, 차례로 ScrollView 내부에 있습니다. 가로 및 세로로 스크롤 할 수있게 해줍니다. 지금은 스크롤하지 않을 헤더 행을 추가합니다. 나는 두 스크롤 뷰가 실제로 TableLayout 내부에 있고 HorizontalScrollView에 TableRow를 추가하기 위해 움직이는 것을 시도했다. 내 희망은 다음 스크롤보기 외부에서 머리글 행을 추가 할 수 있습니다.안드로이드 TableLayout 헤더 행

내가 생각할 수있는 유일한 다른 점은 머리글 행에 대한 두 번째 테이블 레이아웃을 갖는 것입니다. 그러나 줄을 정렬하는 것은 어려운 것처럼 보입니다. 어떤 아이디어?

답변

11

하나의 접근법은 다른 TableLayout의 행에 TableLayout을 포함시키고 아래 보이는 것처럼 앞의 행에 헤더를 넣는 것입니다. 데이터와 헤더를 정렬하려면 header View 객체의 layout_width 속성과 데이터의 View 객체가 동일한 dip 값으로 설정되어야합니다. 또한, 내부 TableLayout의 View 객체의 layout_weight 속성은 해당 헤더와 일치해야합니다. 아래의 XML에서 열 머리글과 일치하도록 한 행의 내부 TableLayout에 3 개의 TextView를 배치했습니다. 이는 정렬을 수행하는 방법을 보여주기위한 것입니다. 레이아웃을 확장하고 런타임에 추가하여 프로그래밍 방식으로 데이터를 채울 수 있습니다.

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


    <TableRow> 
    <TextView android:text="Name" 
     android:layout_width="100dp"   
     android:layout_column="0" 
     android:layout_weight="1"/> 
    <TextView android:text="Score" 
     android:layout_width="30dp" 
     android:layout_column="1" 
     android:layout_weight="1"> 
    </TextView> 
    <TextView android:text="Level" 
     android:layout_width="30dp" 
     android:layout_column="2" 
     android:layout_weight="1"> 
    </TextView> 
    </TableRow> 

    <ScrollView android:layout_height="120dp">  
    <TableLayout android:id="@+id/score_table" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent">   
    <TableRow> 
     <TextView android:text="Al" 
     android:layout_width="100dp"   
     android:layout_column="0" 
     android:layout_weight="1"> 
     </TextView> 
     <TextView android:text="1000" 
     android:layout_width="30dp" 
     android:layout_column="1" 
     android:layout_weight="1"> 
     </TextView> 
     <TextView android:text="2" 
     android:layout_width="30dp" 
     android:layout_column="2" 
     android:layout_weight="1"> 
     </TextView> 
    </TableRow> 
    </TableLayout> 
    </ScrollView>  
</TableLayout> 
+0

는이 http://stackoverflow.com/questions/20241614/android-fixed-header-horizontal-and-vertical-scrolling-table/26211896#26211896 ans와 시도 –

+0

수평 스크롤을 지원하지 않습니다 –

13

실제로이 작업을 수행하는 또 다른 적절한 방법이 생겼습니다.

세로 방향 LinearLayout 내부에서 첫 번째 행으로 머리글 행을 사용하여 테이블을 구축하기 만하면됩니다. 그런 다음 첫 번째 행을 프로그래밍 방식으로 제거한 다음 첫 번째 행을 LinearLayout에 첫 번째 행으로 추가합니다. 이것은 매력처럼 작동했습니다.

편집 : 정적 열 너비를 지정하지 않아도 작동합니다.

+0

호기심이 넘치는 트릭이긴하지만 실제로 작동합니다. – Jonik

+0

이것을 확장 할 수 있습니까? 아마도 일부 코드와 함께? – SoloPilot

+0

(ViewGroup) tableRow.getParent())를 수행했습니다. removeChild (tableRow); 다음 linearLayout.addView (tableRow); 헤더 행을 제거하는 동안 열을 표시하지 않았고 열 너비를 유지하지도 않았습니다. 나는 이것을 onCreate()했다. 다른 위치에 있어야 할 수도 있습니다. – James

3

나는 그 질문이 오래되었다는 것을 안다. 그러나 나는 그 문제가 있었기 때문에 구글이 나에게 주었다. 그리고 더 나은 솔루션을 찾은 것 같아서 공유하고 싶습니다.

아이디어 : ScrollView 내부의 TableLayout을 RelativeLayout에 배치하고 다른 모든 요소 위에 첫 번째 (헤더) 행을 그릴 오버레이를 만듭니다. 여기

가 layout.xml입니다 :

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 

    android:id="@+id/table_wrapper" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
> 
    <ScrollView 

     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     tools:ignore="UselessParent" 
    > 
     <TableLayout 

      android:id="@+id/table" 

      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
     /> 
    </ScrollView> 
</RelativeLayout> 

그리고 여기에 코드입니다 : 당신의 크기를 미리 정의 할 필요 행복하지 사람들을 위해

TableLayout table = (TableLayout)view.findViewById(R.id.table); 

final TableRow headerRow = new TableRow(context); 
table.addView(headerRow); 

table.addView(new TableRow(context)); 
table.addView(new TableRow(context)); 
table.addView(new TableRow(context)); 


RelativeLayout tableWrapper = (RelativeLayout)view.findViewById(R.id.table_wrapper); 

View fakeHeaderView = new View(context) { 
    @Override 
    public void draw(Canvas canvas) { 
     headerRow.draw(canvas); 
    } 
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     int width = headerRow.getMeasuredWidth(); 
     int height = headerRow.getMeasuredHeight(); 

     widthMeasureSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
}; 

tableWrapper.addView(fakeHeaderView); 
+0

위로 엄지! 나는 이것이 어떻게 작동하는지 이해하지 못한다. 그러나 그렇다. 매우 우아하고 실제로 :-) – mmo

+0

이 접근 방식의 문제점은 메모리 누수가 발생한다는 것입니다. 하나는 새로 생성 할 때마다 새 fakeHeaderView를 만들고 tableWrapper에 추가합니다 (자식 목록은 디버거에서 확인할 수있는대로 커지고 커집니다). 그리고 - 이것으로 실험 해본 결과, 이상한 이유로 오래된 인스턴스를 제거 할 수 없거나 추가 한 마지막 인스턴스가 제대로 그려지지 않고 전체 효과가 작동하지 않습니다! 그 어떤 힌트라도? – mmo

0

, 내가 해킹의 비트를 발견 그게 나를 위해 일하고있어.

기본적으로 제목에 대한 별도의 표를 만들어 메인 테이블 위에 배치하지만 동일한 Top 정렬을 사용하여 제목 행의 복사본을 두 개 만들고 기본 표에 하나를 추가 한 후 다른 표를 제목 표를 만들고 하위보기의 layoutParams를 주 표의 행으로 설정하십시오.

다음은 기본적인 예입니다.당신이 당신의 행을 추가 할 때 다음

<HorizontalScrollView 
    android:id="@+id/table_horizontal_scroll_view" 
    android:layout_alignParentTop="true" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:clickable="false"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 

     <ScrollView 
      android:layout_alignParentTop="true" 
      android:layout_marginTop="0dp" 
      android:id="@+id/table_vertical_scroll_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <TableLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/grid_table_layout" 
       /> 

     </ScrollView> 

     <TableLayout 
      android:layout_alignLeft="@+id/table_vertical_scroll_view" 
      android:layout_alignRight="@+id/table_vertical_scroll_view" 
      android:layout_alignStart="@+id/table_vertical_scroll_view" 
      android:layout_alignEnd="@+id/table_vertical_scroll_view" 
      android:layout_alignParentTop="true" 
      android:background="@color/grid_view_background" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/grid_floating_row_layout" 
      /> 

    </RelativeLayout> 


</HorizontalScrollView> 

: 레이아웃에

이있는 ScrollView에서
//clear out any views 
    tableLayout.removeAllViews(); 
    floatingRowLayout.removeAllViews(); 

    TableRow[] rows = getTableContentRows() // content of your table 
    TableRow[] titleRows = {getTitleRow(), getTitleRow()}; //two copies of your title row 
    tableLayout.addView(titleRows[0]); // first add the first title to the main table 
    addRows(rows) // add any other rows 

    floatingRowLayout.addView(titleRows[1]); // floatingRowLayout is connected to id/grid_floating_row_layout 

    titleRows[0].setVisibility(View.INVISIBLE); // make the title row added to the main table invisible 

    // Set the layoutParams of the two title rows equal to each other. 
    // Since this is done after the first is added to the main table, they should be the correct sizes. 
    for(int i = 0; i < titleRows[0].getChildCount(); i++) { 
     titleRows[1].getChildAt(i).setLayoutParams(titleRows[0].getChildAt(i).getLayoutParams()); 
    } 
0

를 사용하여 두 TableLayout을 하나 android:stretchColumns="*"

<RelativeLayout 
      android:layout_width="match_parent" 
      android:orientation="vertical" 
      android:paddingTop="5dp" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/llSpinner"> 
      <TableLayout 
       android:layout_width="match_parent" 
       android:id="@+id/tableHead" 
android:stretchColumns="*" 
       android:layout_height="wrap_content"> 
      </TableLayout> 

     <ScrollView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/tableTotal" 
      android:layout_below="@+id/tableHead" 
      android:id="@+id/scrolltable"> 


     </ScrollView> 
    <TableLayout 
       android:layout_width="match_parent" 
       android:id="@+id/tableTotal" 
android:stretchColumns="*" 
       android:layout_alignParentBottom="true" 
       android:layout_height="wrap_content"> 
      </TableLayout> 
     </RelativeLayout> 

다음 공통 뷰를 생성주고 좋아 ROW와 가장 중요한 언급에 대해 android:layout_width="0dp"

<TableRow 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:focusable="true" 
    android:focusableInTouchMode="false" 
    android:clickable="true" 
    android:background="@android:drawable/list_selector_background"> 

    <TextView 
     android:text="S.No" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/tbsNo" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:layout_column="1" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Date" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:id="@+id/tbDate" 
     android:layout_column="2" /> 


    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Count" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:id="@+id/tbMrCount" 
     android:layout_column="3" /> 
</TableRow> 

이제 활동에

Tablelayout tableHeading =(TableLayout)findViewById(R.id.tableHead); 


       Tablelayout table =(TableLayout) findViewById(R.id.table_repots); 
        trHeading = (TableRow) getLayoutInflater().inflate(R.layout.table_row_item, null); 
       trHeading.setBackgroundColor(Color.parseColor("#6688AC")); 
       trHeading.setPadding(0,10,0,10); 

       TextView tv; 
       tv = (TextView) trHeading.findViewById(R.id.tbsNo); 
       tv.setText("S.No"); 
      tv = (TextView) trHeading.findViewById(R.id.tbDate); 
      tv.setText("Date"); 
      table.addView(tv);