在API级别10或更低级别上运行时,为什么我的TableLayout无法正常显示?

时间:2013-02-17 09:20:45

标签: android xml tablelayout

我的应用使用TableLayout向用户显示一些数据。当我在API级别为11或更高的设备(或AVD)上运行应用程序时,表格会完全按预期显示。

api lvl 11 screenshot

但是,只要我使用API​​级别为10或更低的AVD,TableView就不再显示边距,也不会正确执行android:layout_width="wrap_content" - 标记。

api lvl 8 screenshot

有谁知道,为什么会这样,以及如何解决它?边距不是第一优先(它看起来很丑陋),但内容包装是:由于表没有按预期包装整个内容,用户将无法获得他应该得到的所有信息!

这是我的XML:

<RelativeLayout 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"
    tools:context=".EvalActivity" >       

    <ScrollView
        android:id="@+id/scrollViewGeneral"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"        
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"        
        android:layout_marginBottom="10dp"          
        android:background="@drawable/border_surrounded" >

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp" >

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalNumberOfProblems"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblProblemsCount"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalCorrectSolutions"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblCorrectSolutions"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottomBreak"
                    android:text="@string/str_EvalWrongSolutions"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblWrongSolutions"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalDuration"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblDuration"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalAverageDuration"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblAverageDuration"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalShortestDuration"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblShortestDuration"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottomBreak"
                    android:text="@string/str_EvalLongestDuration"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblLongestDuration"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalAddition"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblAddition"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalSubtraction"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblSubtraction"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalMultiplication"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblMultiplication"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalDivision"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblDivision"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalPower"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblPower"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>

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

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/margin_EvalTableRight"
                    android:layout_marginBottom="@dimen/margin_EvalTableBottom"
                    android:text="@string/str_EvalPercent"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

                <TextView
                    android:id="@+id/lblPercent"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/lbl_OutputDummyString"
                    android:textAppearance="?android:attr/textAppearanceMedium" />

            </TableRow>
        </TableLayout>
    </ScrollView>    

    <ScrollView
        android:id="@+id/scrollViewTable"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"        
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/scrollViewGeneral"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="10dp"        
        android:layout_marginBottom="10dp"          
        android:background="@drawable/border_surrounded" >

        <TableLayout
            android:id="@+id/tableViewTable"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp" >

            <!-- 
            CONTENT GENERATED PROGRAMATICALLY! 
            -->          
        </TableLayout>

    </ScrollView>
 </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

要解决此问题,您需要在表格布局上设置重力。由于您在所有方面都使用了边距,因此您可能需要android:gravity="center"。出于某种原因,API&lt;如果你没有为TableLayout设置重力,那么就会忽略边距。