문제

좋아, 그래서 나는이 tableLayout과 그 데이터가 가득한 데이터를 얻었습니다. 모든 행이 프로그래밍 방식으로 추가되었습니다. horizontalscrollview 내부에 TableLayout을 가지고 있습니다. 결과적으로 ScrollView 내부에 있습니다. 이것은 수평 및 수직으로 스크롤을 제공합니다. 내가 지금하려고하는 것은 스크롤하지 않을 헤더 행을 추가하는 것입니다. 나는 두 스크롤 뷰가 실제로 TableLayout 내부에 있고 horizontalscrollview에 타블러를 추가하도록 물건을 움직이려고 시도했습니다. 내 희망은 스크롤 뷰 외부에 헤더 행을 추가 할 수 있기를 희망했습니다.

내가 생각할 수있는 유일한 것은 헤더 행에 대한 두 번째 테이블 레이아웃을 갖는 것입니다. 그러나 열을 줄이기가 어려운 것 같습니다. 어떤 아이디어?

도움이 되었습니까?

해결책

한 가지 방법은 다른 TableLayout의 행 내에 TableLayout을 포함시키고 아래에 표시된대로 앞의 행에 헤더를 넣는 것입니다. 데이터와 헤더를 정렬하려면 헤더 뷰 객체의 Layout_width 속성과 데이터 뷰 개체가 동일한 딥 값으로 설정되어야합니다. 또한 내부 TableLayout의 View Objects의 Layout_weight 속성은 해당 헤더와 일치해야합니다. 이제 아래 XML에서 열 헤더와 일치하도록 내부 탁자에 3 개의 텍스트 뷰를 단일 행에 배치했습니다. 이것은 단지 정렬을 수행 할 수있는 방법을 보여주기위한 것입니다. 레이아웃을 팽창시키고 런타임에 추가하여 해당 데이터를 프로그래밍 방식으로 채울 수 있습니다.

<?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>

다른 팁

나는 실제로 이것을하는 또 다른 괜찮은 방법을 생각해 냈습니다.

수직 방향 LinearLayout 내부의 첫 번째 행으로 헤더 행을 사용하여 테이블을 정상적으로 작성하십시오. 다음으로, 프로그래밍 방식으로 첫 번째 행을 제거한 다음 선형 층에 첫 번째 자식으로 추가하십시오. 이것은 매력처럼 작동했습니다.

편집 : 정적 열 폭을 지정하지 않고도 작동합니다.

나는 그 질문이 오래되었음을 알고 있지만, Google이 같은 문제를 겪었을 때 나에게 준 것은 첫 번째 문제였습니다. 더 나은 솔루션을 찾았다 고 생각하기 때문에 공유하고 싶습니다.

아이디어 : TableLayout (ScrollView 내부)를 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);

당신의 크기를 사전 정의 해야하는 것에 만족하지 않는 사람들을 위해, 나는 나를 위해 일하는 약간의 해킹을 발견했습니다.

기본적으로 제목에 대한 별도의 테이블을 만들어 메인 테이블 위에 놓지만 동일한 상단 정렬로 제목 행의 두 편의 사본을 만들고 메인 테이블에 하나를 추가 한 후 다른 하나를 제목 테이블에 추가하고 세트하십시오. Child View의 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>

그런 다음 행을 추가 할 때 :

    //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());
    }

Give와 같은 스크롤 뷰에서 하나를 사용하십시오 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>

그런 다음 행에 대한 일반적인보기와 가장 중요한 언급을 만듭니다. 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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top