简体   繁体   中英

TableLayout width does not match RelativeLayout width

I am a beginner android programmer with a fair amount of Java background, mostly on a mainframe. (Yes it runs on a mainframe). I writing an app that tracks some daily activity, and after the default RealativeLayout I am adding a TableLayout via XML. In my onWindowFocusChanged I am also trying to set the width of the TableLayout. When I measure the two, the RelativeLayout is 1024, the screen size, but the TableLayout is 960. The XML and code snippet are below. Any help would be greatly appreciated as I have done many searches and a lot of Doc, and still can't get this to match.

    <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 code

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

If you only want your table layout width match RelativeLayout width you shouldn't need to support multiple xml element. Your problem maybe cause when you support android:padding in RelativeLayout or android:layout_margin in TableLayout . Check the simple code below

<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>   

See more about android layout margin and padding in this Hope this help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM