如何将EditText的高度设置为最大值减去其他按钮和EditText的高度?

时间:2013-11-22 01:10:41

标签: android android-layout height

我想用我的EditText视图填充屏幕,其中所有屏幕高度减去按钮的高度,EditText

中的Activity s

我的活动是xml

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <Button
            android:id="@+id/pickcontact_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="pickContact"
            android:text="@string/pickcontact_string" />
    </TableRow>

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

        <TextView
            android:id="@+id/chosen_contact"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/contact_default_string"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/chosen_contact_tel_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/contact_default_number"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>

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

        <EditText
            android:id="@+id/edit_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="@string/edit_message"
            android:minHeight="100dp"
            android:gravity="bottom" />
    </TableRow>

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

        <Button
            android:id="@+id/button_send"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="sendMessage"
            android:text="@string/button_send" />


        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="sendEmail"
            android:text="@string/send_email_btn" />
    </TableRow>

</TableLayout>

2 个答案:

答案 0 :(得分:0)

试试这个:

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:id="@+id/tableRow_pickcontact_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <!-- Blah blah-->
    </TableRow>

    <TableRow
        android:id="@+id/tableRow_chosen_contact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <!-- Blah blah-->
    </TableRow>

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

        <EditText
            android:id="@+id/edit_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            <!-- android:layout_weight="1" YOU DON'T NEED THAT-->
            android:hint="@string/edit_message"
            android:minHeight="100dp"
            android:gravity="bottom" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
        <!-- Blah blah-->
    </TableRow>
</TableLayout>

重点是,将EditText的宽度和高度父布局设为fill_parent。

这样做会使得之后的所有元素都不可见,因为布局已经“填充”了父元素,因此您还应该为这些元素设置权重,以确保它们仍然可见。

虽然这样做时需要找到合适的重量,但在此之前制作这些元素会更容易(但是你需要改变你的设计)。

答案 1 :(得分:0)

// try this
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

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

        <Button
            android:id="@+id/pickcontact_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="pickContact"
            android:text="@string/pickcontact_string" />
    </TableRow>

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

        <TextView
            android:id="@+id/chosen_contact"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/contact_default_string"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/chosen_contact_tel_number"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/contact_default_number"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </TableRow>

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

        <EditText
            android:id="@+id/edit_message"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:hint="edit_message"
            android:gravity="bottom" />
    </TableRow>

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

        <Button
            android:id="@+id/button_send"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="sendMessage"
            android:text="@string/button_send" />


        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="sendEmail"
            android:text="@string/send_email_btn" />
    </TableRow>

</TableLayout>
相关问题