首页 > 代码库 > tablelayout高度问题

tablelayout高度问题

============问题描述============


我想让tablelayout里面的所有组件的高度加起来刚好占满屏幕,应该怎么设置?
<TableLayout 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:textSize="50sp" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <EditText
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/result_bg"
            android:editable="false"
            android:layout_span="4" 
            android:gravity="right|center_vertical"
            android:layout_margin="10dp"
            android:layout_weight="1" >
<requestFocus />
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/delete"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="del" 
            android:layout_weight="1" />

        <Button
            android:id="@+id/equal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="2"
            android:text="="
            android:layout_weight="1" />

    </TableRow>
  
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/number1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="3"
            android:layout_weight="1" />

        <Button
            android:id="@+id/add"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="+"
            android:layout_weight="1" />

    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/number4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="4"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="5"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="6"
            android:layout_weight="1" />

        <Button
            android:id="@+id/minus"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="-"
            android:layout_weight="1" />

    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/number7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="7"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="8"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="9"
            android:layout_weight="1" />

        <Button
            android:id="@+id/multiply"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="*"
            android:layout_weight="1" />

    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/ac"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ac"
            android:layout_weight="1" />
        <Button
            android:id="@+id/number0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="0"
            android:layout_weight="1" />

        <Button
            android:id="@+id/dot"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="." 
            android:layout_weight="1" />

        <Button
            android:id="@+id/divide"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="/"
            android:layout_weight="1" />

    </TableRow>

</TableLayout>
目前效果是这样

============解决方案1============


修改TableRow的layout_height,增加layout_weight:
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">


根据需要修改TableRow中的控件的layout_height:
android:layout_height="match_parent"


修改后的xml:
<TableLayout 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:textSize="50sp" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >
        <EditText
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:background="@drawable/result_bg"
            android:editable="false"
            android:layout_span="4" 
            android:gravity="right|center_vertical"
            android:layout_margin="10dp"
            android:layout_weight="1" >
			<requestFocus />
        </EditText>

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/delete"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_span="2"
            android:text="del" 
            android:layout_weight="1" />

        <Button
            android:id="@+id/equal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_span="2"
            android:text="="
            android:layout_weight="1" />

    </TableRow>
  
    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/number1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="1"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="2"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number3"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="3"
            android:layout_weight="1" />

        <Button
            android:id="@+id/add"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="+"
            android:layout_weight="1" />

    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/number4"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="4"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number5"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="5"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number6"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="6"
            android:layout_weight="1" />

        <Button
            android:id="@+id/minus"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="-"
            android:layout_weight="1" />

    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow5"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/number7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="7"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number8"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="8"
            android:layout_weight="1" />

        <Button
            android:id="@+id/number9"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="9"
            android:layout_weight="1" />

        <Button
            android:id="@+id/multiply"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="*"
            android:layout_weight="1" />

    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow6"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <Button
            android:id="@+id/ac"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="ac"
            android:layout_weight="1" />
        <Button
            android:id="@+id/number0"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="0"
            android:layout_weight="1" />

        <Button
            android:id="@+id/dot"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="." 
            android:layout_weight="1" />

        <Button
            android:id="@+id/divide"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="/"
            android:layout_weight="1" />

    </TableRow>

</TableLayout>

tablelayout高度问题