2013-07-07 36 views
2

在android編程中,如何將佈局高度更改爲最小,以使所有元素仍然顯示在其上。如何更改佈局以在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按鈕現在出現的取消按鈕旁邊。我希望它對齊。

回答

1

使用下面的代碼

android:layout_height="wrap_content" 

你真正確保您的佈局看你想要的方式在地球上每一個Android手機上

對於間距,Android定義了兩個屬性:android:layout_margin和android:padding。 android:layout_margin屬性定義容器的間距,而android:padding則定義視圖的間距。

機器人:填充 - 定義的內容的間隔上的控制的所有四個側面。要分別定義每邊的填充,請使用android:paddingLeft,android:paddingRight,android:paddingTop和android:paddingBottom。

機器人:paddingTop - 定義的內容和所述控制

機器人的頂部之間的間距: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> 
+0

你能告訴我新的代碼嗎? – omega

+0

明確告訴我你想要什麼........你已經在你的相對佈局xml代碼中使用了android:layout_height =「wrap_content」 –

+0

嘗試添加...在你的代碼中填充...查看編輯答案 –

-1

使用的LinearLayout和刪除邊緣,就像這樣:

<?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> 
+0

更好的控制這個,我得到了錯誤,所以我不得不將match_content改成wrap_content,然後所有的輸入字段和文字瀏覽和按鈕都相互重疊。 – omega

+0

實際上,我添加了水平方向,這有幫助。但是現在OK按鈕出現在取消按鈕的旁邊。我想確定按鈕是否對齊。我用新代碼更新了我的第一篇文章。 – omega

0

添加機器人: layout_weight =「1」給cancel和ok按鈕,使它們在水平LinearLayout上相鄰顯示。

你也可以使用一個RelativeLayout的替代,這將授予您的按鈕的佈局方式,你可以當我試圖右對齊,左,說哪一個是正確的,等

相關問題