2

在android编程中,如何将布局高度更改为最小的,以使所有元素仍显示在其上。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/email" />

    <EditText
        android:id="@+id/textinput_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <TextView
        android:id="@+id/textview_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/password" />

    <EditText
        android:id="@+id/textinput_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
        <Button
            android:id="@+id/button_exit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+string/cancel" />

        <Button
            android:id="@+id/button_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+string/ok" />
    </LinearLayout>

</LinearLayout>

这是我的代码,当我运行它时,会显示很多空白。此布局仅显示在对话框中。

谢谢。

编辑:

我得到了最小化的高度,但 OK 按钮现在出现在取消按钮旁边。我希望它正确对齐。

4

3 回答 3

1

使用下面的代码时

 android:layout_height="wrap_content"

您实际上确保您的布局在地球上的每部 Android 手机上看起来都符合您的要求

对于间距,Android 定义了两个属性:android:layout_margin 和 android:padding。android:layout_margin 属性定义容器的间距,而 android:padding 定义视图的间距。

android:padding——定义控件所有四个边的内容间距。要单独为每一侧定义填充,请使用 android:paddingLeft、android:paddingRight、android:paddingTop 和 android:paddingBottom。

android:paddingTop——定义内容与控件顶部的间距

android:paddingBottom——定义内容和控件底部之间的间距。

android:paddingLeft——定义内容和控件左侧之间的间距。

android:paddingRight——定义内容和控件右侧之间的间距。

*编辑后的代码请务必将名称“ mainactivity 更改为您的活动名称” *

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

       <TextView
    android:id="@+id/textview_username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+string/email" />

<EditText
    android:id="@+id/textinput_username"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textEmailAddress" />

<TextView
    android:id="@+id/textview_password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+string/password" />

<EditText
    android:id="@+id/textinput_password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPassword" />       

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
    <Button
        android:id="@+id/button_exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/cancel" />

    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/ok" />
</LinearLayout>


</RelativeLayout>

您的答案完全有效的代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/email" />

    <EditText
        android:id="@+id/textinput_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <TextView
        android:id="@+id/textview_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/password" />

    <EditText
        android:id="@+id/textinput_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="28.05" >

        <Button
            android:id="@+id/button_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:gravity="center_vertical"
            android:text="@+string/ok" />

        <Button
            android:id="@+id/button_exit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="@+string/cancel" />

    </RelativeLayout>

</LinearLayout>
于 2013-07-07T23:08:50.187 回答
0

将 android:layout_weight="1" 添加到取消和确定按钮,使它们在水平线性布局上一个挨一个出现。

您也可以改用 RelativeLayout,这样您可以更好地控制按钮的布局方式,您可以右对齐、左对齐、说出哪个在右边等等。

于 2013-07-07T23:32:51.693 回答
-1

使用线性布局并删除边距,如下所示:

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

    <TextView
        android:id="@+id/textview_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/email" />

    <EditText
        android:id="@+id/textinput_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <TextView
        android:id="@+id/textview_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/password" />

    <EditText
        android:id="@+id/textinput_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <Button
        android:id="@+id/button_exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/cancel" />

    <Button
        android:id="@+id/button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+string/ok" />

</LinearLayout>
于 2013-07-07T23:07:25.353 回答