【问题标题】:horizontal scrolling is not working in table layout in android水平滚动在 android 的表格布局中不起作用
【发布时间】:2016-05-18 17:35:36
【问题描述】:

我有一个定义了 5 列的表格布局。但是由于第二列中的大文本,内容正在离开屏幕。所以我试图放置一个水平滚动条。却无能为力。我有一个以 Horizo​​ntalScrollView 作为父级的 TableLayout。布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Balance_Inquiry" >

    <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"                           
                   android:layout_gravity="center_horizontal" >

                <HorizontalScrollView
                    android:id="@+id/horizontalView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dip"
                    android:scrollbars="horizontal|vertical" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent" >

                        <TableLayout
                            android:id="@+id/table_layout_balance_inquiry"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_weight="1.0"
                            android:stretchColumns="0,1,2,3,4,5" >
                        </TableLayout>
                    </LinearLayout>
                </HorizontalScrollView>
            </LinearLayout>
</LinearLayout>

现在我将数据添加到此表格布局中,如下所示:

private void addHeader() {
        // TODO Auto-generated method stub
         TableRow tr_head = new TableRow(this);
         tr_head.setId(10);
         tr_head.setBackgroundColor(getResources().getColor(R.color.bg_login));
         tr_head.setLayoutParams(new LayoutParams(
                 LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

         TextView labelSrNo = new TextView(this);
         labelSrNo.setId(20);
         labelSrNo.setText("SL NO.");
         labelSrNo.setTextColor(Color.WHITE);
         labelSrNo.setPadding(5, 5, 5, 5);
         tr_head.addView(labelSrNo);// add the column to the table row here

         TextView labelACCNO = new TextView(this);
         labelACCNO.setId(21);// define id that must be unique
         labelACCNO.setText("ACC NO."); // set the text for the header 
         labelACCNO.setTextColor(Color.WHITE); // set the color
         labelACCNO.setPadding(5, 5, 5, 5); // set the padding (if required)
         tr_head.addView(labelACCNO); // add the column to the table row here

         TextView labelACCTYPE = new TextView(this);
         labelACCTYPE.setId(20);
         labelACCTYPE.setText("ACC TYPE");
         labelACCTYPE.setTextColor(Color.WHITE);
         labelACCTYPE.setPadding(5, 5, 5, 5);
         tr_head.addView(labelACCTYPE);// add the column to the table row here

         TextView labelAVAILBLNCE = new TextView(this);
         labelAVAILBLNCE.setId(21);// define id that must be unique
         labelAVAILBLNCE.setText("AVAIL BALANCE"); // set the text for the header 
         labelAVAILBLNCE.setTextColor(Color.WHITE); // set the color
         labelAVAILBLNCE.setPadding(5, 5, 5, 5); // set the padding (if required)
         tr_head.addView(labelAVAILBLNCE); // add the column to the table row here

         TextView extraBalance = new TextView(this);
         extraBalance.setId(21);// define id that must be unique
         extraBalance.setText("AVAIL BALANCE"); // set the text for the header 
         extraBalance.setTextColor(Color.WHITE); // set the color
         extraBalance.setPadding(5, 5, 5, 5); // set the padding (if required)
         tr_head.addView(extraBalance); 

         table_layout_balance_inquiry.addView(tr_head, new TableLayout.LayoutParams(
                 LayoutParams.MATCH_PARENT,
                 LayoutParams.WRAP_CONTENT));
    }

但是水平滚动不起作用。如何启用水平滚动?任何建议都有很大帮助。

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    试试这个代码:-

       <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
    <HorizontalScrollView
        android:id="@+id/hscrll1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal">
    
            <TableLayout
                android:id="@+id/table_main_detail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@layout/shape">
    
            </TableLayout>
        </LinearLayout>
    </HorizontalScrollView>
    </ScrollView>
    

    这目前对我有用。

    【讨论】:

      【解决方案2】:

      尝试为 LinearLayout 父级和 Horizo​​nalScrollView 设置 android:layout_width="match_parent"

      【讨论】:

        猜你喜欢
        • 2011-11-22
        • 1970-01-01
        • 2020-11-08
        • 2013-05-13
        • 1970-01-01
        • 2018-10-04
        • 2013-10-18
        • 2013-11-11
        • 1970-01-01
        相关资源
        最近更新 更多