سؤال

حسنا، لذلك لدي هذا التلقيح، ومليء بالبيانات - مع كل الصفوف المضافة برمجيا. لقد حصلت على TableLayout داخل HorizontalscrolleView، والتي بدورها داخلي داخل التمرير - وهذا يمنحني التمرير على حد سواء أفقيا وعموديا. ما أحاول القيام به الآن هو إضافة صف رأس لذلك لن تمرير. لقد حاولت نقل الأشياء حولها حتى تكون كل من وجهات نظرتي التمرير في الواقع داخل TableLayout وإضافة Tablerows إلى HorizontalscrolleView؛ كان أملي قادرا بعد ذلك إضافة صف الرأس خارج آراء التمرير.

الشيء الآخر الوحيد الذي يمكنني التفكير فيه هو وجود تخطيط طاولة ثان فقط للصف الرأس، ولكن يبدو أن الحصول على الأعمدة التي يجب أن يصبح صعوبة. أيه أفكار؟

هل كانت مفيدة؟

المحلول

نهج واحد هو من خلال تضمين TableLayout داخل صف آخر TableLayout ووضع الرأس في الصف السابق كما هو موضح أدناه. يتطلب محاذاة البيانات والرئيس الخاصة بخاصية Layout_width لكائنات عرض الرأس وكائنات عرض البيانات المراد تعيينها على نفس قيم تراجع. أيضا، يجب أن تطابق خاصية Layout_weight لكائنات عرض TableLayout الداخلية في رأسها المقابل. الآن، في XML أدناه، وضعت 3 نص نصي في TableLayout Inner TableLayout في صف واحد لتتناسب مع رؤوس الأعمدة. هذا فقط لإظهار كيف يمكن القيام بمحاذاة. يمكنك ملء هذه البيانات برمجيا عن طريق تضخيم تخطيط وإضافة ذلك في وقت التشغيل.

<?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. بعد ذلك، قم برمجيا بإزالة الصف الأول ثم أضفه كأول طفل إلى Linearlayout. عملت هذا وكأنه سحر.

تحرير: هذا يعمل أيضا دون الحاجة إلى تحديد عرض الأعمدة الثابتة.

أعلم أن السؤال قديم، لكنه كان أول واحد، أن جوجل أعطاني كما كان لدي نفس المشكلة. وبما أنني أعتقد أنني وجدت حلا أفضل، أود مشاركتها.

الفكرة: ضع 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);

بالنسبة لأولئك الذين ليسوا سعداء بالضغط على ما قبل تحديد أحجامك، وجدت قليلا من الاختراق الذي يعمل بالنسبة لي.

في الأساس، قم بإجراء جدول منفصل للعنوان ووضعه على طاولتك الرئيسية، ولكن مع نفس المحاذاة الأعلى، ثم قم بإنشاء نسختين من صف العنوان وبعد إضافة واحد إلى الجدول الرئيسي، أضف الآخر إلى جدول العنوان وتعيين تشكل 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());
    }

استخدم اثنين من TableLayout One in ScrollView مثل إعطاء 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