【问题标题】:How to set EditText's height to maximum minus other buttons' and EditText's heights?如何将 EditText 高度设置为最大减去其他按钮和 EditText 高度?
【发布时间】:2013-11-22 12:09:40
【问题描述】:

我想用我的EditText 视图填充屏幕,在这个Activity 中,所有屏幕高度减去按钮和EditTexts 的高度

我的活动的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>

【问题讨论】:

  • 你能发一个你想要设计的屏幕截图吗?

标签: android android-layout height


【解决方案1】:

试试这个:

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

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

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

【讨论】:

    【解决方案2】:
    // 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>
    

    【讨论】:

    • 我将其余的屏幕高度分配给这一行“tableRow_message”
    猜你喜欢
    • 1970-01-01
    • 2016-07-27
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-14
    相关资源
    最近更新 更多