Java 表布局不适合屏幕

Java 表布局不适合屏幕,java,android,background,tablelayout,Java,Android,Background,Tablelayout,我有一个动态创建表的活动。活动已创建,并且工作正常 这是xml文件的源代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent"

我有一个动态创建表的活动。活动已创建,并且工作正常

这是xml文件的源代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#142c57"
    tools:context=".TableActivity" >

    <TextView
        android:id="@+id/heading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="@string/followup_list" 
        android:textColor="#FFFFFF"
        android:textSize="15sp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/heading_textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" 
        android:padding="3dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" 
            android:background="@drawable/inner_box">

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" >

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

                    <HorizontalScrollView
                        android:id="@+id/horizontalScrollView1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true" >

                        <TableLayout
                            android:id="@+id/follow_up_table"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent" 
                            android:stretchColumns="0">


                        </TableLayout>

                    </HorizontalScrollView>

                </RelativeLayout>

            </ScrollView>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

xml文件的输出:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#142c57"
    tools:context=".TableActivity" >

    <TextView
        android:id="@+id/heading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="@string/followup_list" 
        android:textColor="#FFFFFF"
        android:textSize="15sp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/heading_textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" 
        android:padding="3dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" 
            android:background="@drawable/inner_box">

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" >

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

                    <HorizontalScrollView
                        android:id="@+id/horizontalScrollView1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true" >

                        <TableLayout
                            android:id="@+id/follow_up_table"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent" 
                            android:stretchColumns="0">


                        </TableLayout>

                    </HorizontalScrollView>

                </RelativeLayout>

            </ScrollView>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>


我想让桌子填满整个盒子,桌子右侧的空间不是必需的,看起来很尴尬。要做到这一点,需要做些什么?

您的表格布局宽度包含其内容,它应该是
匹配\u parent

<TableLayout
 android:id="@+id/follow_up_table"
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:stretchColumns="0">


</TableLayout>

按照以下步骤进行更改:

<TableLayout
    android:id="@+id/follow_up_table"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" 
    android:stretchColumns="0">
</TableLayout>

使用HorizontalScrollView的目的是什么?只需将其删除,并将TableLayout的android:layout\u宽度设置为与父项匹配。

需要像这样编辑布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#142c57"
    tools:context=".TableActivity" >

    <TextView
        android:id="@+id/heading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="@string/followup_list" 
        android:textColor="#FFFFFF"
        android:textSize="15sp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/heading_textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" 
        android:padding="3dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" 
            android:background="@drawable/inner_box">

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" >

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

                    <TableLayout
                        android:id="@+id/follow_up_table"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true" 
                        android:stretchColumns="0,1">


                    </TableLayout>

                </RelativeLayout>

            </ScrollView>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

输出

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#142c57"
    tools:context=".TableActivity" >

    <TextView
        android:id="@+id/heading_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="@string/followup_list" 
        android:textColor="#FFFFFF"
        android:textSize="15sp"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/heading_textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp" 
        android:padding="3dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" 
            android:background="@drawable/inner_box">

            <ScrollView
                android:id="@+id/scrollView1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true" >

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

                    <TableLayout
                        android:id="@+id/follow_up_table"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:layout_alignParentTop="true" 
                        android:stretchColumns="0,1">


                    </TableLayout>

                </RelativeLayout>

            </ScrollView>

        </RelativeLayout>

    </RelativeLayout>

</RelativeLayout>

更好地拉伸和收缩所有列,以适应您的屏幕:

<TableLayout
 android:id="@+id/follow_up_table"
 android:layout_width="match_parent"
 android:layout_height="match_parent" 
 android:stretchColumns="*"
 android:shrinkColumns="*">

</TableLayout>

发现差异并不容易,让我来发现它。。。android:stretchColumns=“0,1”