TableLayout宽度与RelativeLayout宽度不匹配

时间:2015-09-30 18:53:14

标签: android android-layout width tablelayout

我是一名初学的android程序员,拥有相当数量的Java背景,主要是在大型机上。 (是的,它在大型机上运行)。我编写了一个跟踪一些日常活动的应用程序,在默认的RealativeLayout之后,我通过XML添加了一个TableLayout。在我的onWindowFocusChanged中,我也试图设置TableLayout的宽度。当我测量两个时,RelativeLayout是1024,屏幕大小,但TableLayout是960.下面是XML和代码片段。任何帮助都会非常感激,因为我已经完成了很多搜索和很多文档,但仍然无法匹配。

    <TableLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/WeekTbar"
    android:stretchColumns="*"
    android:shrinkColumns="*"
    android:layout_alignParentLeft="true"
    android:id="@+id/table"
    android:background="#d4fbff">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0ff2D2Bf"
        >

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_column="0"
            android:background="#2d2bff"
            android:textColor="#ffffff"
            android:text="        " />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_column="1"
            android:text="Sun"
            android:textColor="#ffffff"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText3"
            android:layout_column="2"
            android:text="Mon"
            android:background="#2d2bff"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText4"
            android:layout_column="3"
            android:text="Tue"
            android:textColor="#ffffff"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText6"
            android:layout_column="4"
            android:text="Wed"
            android:background="#2d2bff"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText5"
            android:layout_column="5"
            android:text="Thu"
            android:textColor="#ffffff"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText8"
            android:layout_column="6"
            android:text="Fri"
            android:background="#2d2bff"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText9"
            android:layout_column="7"
            android:text="Sat"
            android:textColor="#ffffff"
            android:background="#2d2bff" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0ff2D2Bf"
        android:focusableInTouchMode="true">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText10"
            android:layout_column="0"
            android:text="Steps"
            android:background="#2d2bff"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/SunSteps"
            android:layout_column="1"
            android:background="#2d2bff"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/MonSteps"
            android:layout_column="2"
            android:textColor="#ffffff"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TueSteps"
            android:layout_column="3"
            android:background="#2d2bff"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/WedSteps"
            android:layout_column="4"
            android:textColor="#ffffff"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ThuSteps"
            android:layout_column="5"
            android:background="#2d2bff"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/FriSteps"
            android:layout_column="6"
            android:textColor="#ffffff"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/SatSteps"
            android:layout_column="7"
            android:background="#2d2bff" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0ff2D2Bf">


        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText11"
            android:layout_column="0"
            android:text=Cals"
            android:textColor="#ffffff"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/SunCals"
            android:layout_column="1"
            android:focusableInTouchMode="true"
            android:background="#2d2bff"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/MonCals"
            android:layout_column="2"
            android:textColor="#ffffff"
            android:focusableInTouchMode="true"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/TueCals"
            android:layout_column="3"
            android:background="#2d2bff"
            android:focusableInTouchMode="true"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/WedCals"
            android:layout_column="4"
            android:textColor="#ffffff"
            android:focusableInTouchMode="true"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ThuCals"
            android:layout_column="5"
            android:background="#2d2bff"
            android:focusableInTouchMode="true"
            android:textColor="#ffffff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/FriCals"
            android:layout_column="6"
            android:textColor="#ffffff"
            android:focusableInTouchMode="true"
            android:background="#2d2bff" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText7"
            android:layout_column="7"
            android:background="#2d2bff"
            android:focusableInTouchMode="true"
            android:textColor="#ffffff" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#0ff2D2Bf">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText12"
            android:layout_column="0"
            android:text="Wgt"
            android:background="#2d2bff"
            android:textColor="#ffffff" />
    </TableRow>

</TableLayout>

Java代码

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus == true) {
        TableLayout table = (TableLayout) findViewById(R.id.table);
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.RelativeLayout);
        int layoutWidth = rl.getWidth();
        int layoutHeight = rl.getHeight();
        table.getLayoutParams().width = layoutWidth;
        table.setMinimumWidth(layoutWidth);
        gw = table.getWidth();
        rw = layoutWidth;
        int x =1;
    }
}

1 个答案:

答案 0 :(得分:0)

如果您只希望表格布局宽度与RelativeLayout宽度匹配,则不需要支持多个xml元素。 当您在android:paddingRelativeLayout android:layout_margin中支持TableLayout时,可能会导致您的问题。 检查下面的简单代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:padding="10dp" // remove it
        android:layout_height="match_parent" >
        ...
        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ff0000"
            android:layout_margin="10dp"  // remove it
         >

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

            </TableRow>
            ....
        </TableLayout>
    </RelativeLayout>   

this中查看有关Android布局边距和填充的更多信息 希望这个帮助

相关问题