水平滚动在android

时间:2016-05-18 07:24:15

标签: android android-layout

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

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

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

2 个答案:

答案 0 :(得分: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>

目前这对我有用。

答案 1 :(得分:0)

为LinearLayout parent和Horizo​​nalScrollView

尝试设置android:layout_width="match_parent"