2017-04-19 127 views
0

我試圖用登錄表單做一個簡單的應用程序。Android - 如何支持多種屏幕尺寸?

我的問題是,它看起來不錯,在Nexus 4,但在Nexus 5或Nexus 7的佈局是不是我想要的......

Nexus 4

Nexus 7

什麼我需要做一個適用於Nexus S,Nexus 4,5,7等的佈局。

XML

 <?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/AMOB_gray" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.tiagosilva.amob_android.Log" 
    tools:showIn="@layout/activity_log"> 

<LinearLayout 
    android:layout_width="368dp" 
    android:layout_height="495dp" 
    android:orientation="vertical" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    tools:layout_editor_absoluteX="8dp" 
    tools:layout_editor_absoluteY="8dp"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/imageView2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:srcCompat="@drawable/logo_amob" /> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:layout_editor_absoluteX="8dp" 
      tools:layout_editor_absoluteY="8dp"> 

      <EditText 
       android:id="@+id/et_email" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_alignParentTop="true" 
       android:layout_marginTop="122dp" 
       android:background="@drawable/round" 
       android:ems="10" 
       android:hint="Email" 
       android:inputType="textEmailAddress" 
       android:padding="10dp" /> 

      <EditText 
       android:id="@+id/et_password" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:layout_centerVertical="true" 
       android:background="@drawable/round" 
       android:ems="10" 
       android:hint="Password" 
       android:inputType="textPassword" 
       android:padding="10dp" /> 

      <Button 
       android:id="@+id/btn_login" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentEnd="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentStart="true" 
       android:layout_below="@+id/et_password" 
       android:layout_marginTop="13dp" 
       android:background="@color/AMOB_yellow" 
       android:text="Login" 
       android:textColor="@color/AMOB_gray" /> 

      <TextView 
       android:id="@+id/tv_recuperarPassword" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/btn_login" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="17dp" 
       android:text="Recuperar Password" 
       android:textColor="@android:color/white" 
       android:textSize="17sp" /> 

      <Button 
       android:id="@+id/btn_noAccount" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentEnd="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentStart="true" 
       android:text="No Account yet? Register here!" 
       android:background="#ff0000"/> 

     </RelativeLayout> 

    </LinearLayout> 

</LinearLayout> 

+0

「_Questions尋求幫助調試(」爲什麼不是這個代碼的工作?「)必須包括所期望的行爲,一個特定的問題或錯誤的和必要的最短的代碼在問題本身中重現它沒有明確問題陳述的問題對其他讀者沒有用請參閱:如何創建一個最小,完整和可驗證的示例_「 – csmckelvey

+1

您是否至少瀏覽了Android文檔中的培訓部分:https://developer.android.com/training/multiscreen/index.html –

回答

0

屬性android:layout_widthandroid:layout_height確定UI組件大小,obvius。有時你應該將它們設置爲wrap_contentmatch_parent,而不是固定大小,以適應設備屏幕大小。

就讓我們來看看在你的佈局,你可以設置android:layout_widthmatch_parent

以前

android:layout_width="368dp"

android:layout_width="match_parent"

+0

謝謝。基本上我永遠不能用一個固定的數字來設置寬度和高度嗎? – adiajdiadj

+0

這取決於你的設計,在某些情況下,你應該把一個固定的值,但要在其他設備上確定,或者你考慮使用相對佈局,比如'RelativeLayout','ConstraintLayout'等......或者讓'View's auto根據寬度和高度調節。 – betorcs

+0

在Android Studio的佈局編輯器中,您可以選擇不同的設備,根據屏幕尺寸檢查佈局行爲。 – betorcs

0

您可以使用SDP庫而不是DP。

此外,使用的RelativeLayout或LinearLayout中,而不是ConstraintLayout