Android: Allineare il centro LinearLayout per tutte le size dello schermo

Vorrei progettare un layout per la mia schermata di login, ho provato i seguenti codici. Utilizzando alcuni valori codificati ho ottenuto il mio centro centro di colore verde per alcune size dello schermo. So che utilizzando i valori hardcoded non funzionano su formati di schermo diversi. Ho provato numerose cose su SO. Fornire il modo migliore immettere qui la descrizione dell'immagine

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/register_header" android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal" > <ImageView android:id="@+id/title_image" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:gravity="center" android:src="@drawable/logo_blue" /> </RelativeLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginLeft="150dp" android:layout_marginRight="150dp" android:layout_marginTop="50dp" android:layout_marginBottom="50dp" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/UserInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/WelcomeInfo" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/user" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="User Name" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enter username" android:singleLine="true" android:textColor="#000000" > </EditText> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/PassInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/UserInfo" android:orientation="horizontal" > <TextView android:id="@+id/pwd" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Password" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enterpassword" android:singleLine="true" android:textColor="#000000" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/StoreNumInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/WelcomeInfo" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/num" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Store Number" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/store_num" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enter the Store number" android:imeOptions="actionDone" android:password="true" android:textColor="#000000" > </EditText> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Options" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/PassInfo" android:orientation="horizontal" > <CheckBox android:id="@+id/signed_in" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Stay Signed-in" android:textColor="#000000" /> <Button android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Login" android:textSize="28dp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> </LinearLayout> </ScrollView> 

  • Aggiunta di Google Guava al progetto Android - rallenta in modo significativo la build
  • Leggi la velocità di SharedPreferences
  • Applicare molti filtri di colore allo stesso disegnatore
  • Elenco impostazione dell'adattatore - Come posso non ripetere la risorsa di layout dell'elenco degli elenchi?
  • Disegnare un'image sopra un field di text android
  • Android, riguardo al design dell'interface utente
  • Visualizzare l'image dai problemi di URL e di orientamento dello schermo
  • LocationClient getLastLocation () restituisce null
  • Cercando di aggiungere adb a PATH variabile OSX
  • Login di Facebook e al server web
  • riprodurre lo streaming in VideoView, convertire url in rtsp
  • Videocamera di logging di Android, ma gioca a testa in giù
  • 5 Solutions collect form web for “Android: Allineare il centro LinearLayout per tutte le size dello schermo”

    Utilizza la copertina del tuo layout verde come RelativeLayout. e quindi definire il tuo greenLayout come questo. android:layout_centerHorizontal="true" android:layout_centerVertical="true"

    Guarda il mio xml può centrare tutto lo schermo

     <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/lin_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:orientation="vertical" > </LinearLayout> </RelativeLayout> 

    Prova questo:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > // put all your xml code here </LinearLayout> 

    Prova il codice modificato:

    NUOVO EDIT:

     <?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="match_parent" android:gravity="center" android:orientation="vertical" android:weightSum="1"> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/register_header" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="0.1" android:orientation="horizontal" > <ImageView android:id="@+id/title_image" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:gravity="center" android:src="@drawable/logo_blue" /> </RelativeLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_weight="0.9" android:gravity="center" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/UserInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/WelcomeInfo" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/user" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="User Name" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/username" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enter username" android:singleLine="true" android:textColor="#000000" > </EditText> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/PassInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/UserInfo" android:orientation="horizontal" > <TextView android:id="@+id/pwd" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Password" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enterpassword" android:singleLine="true" android:textColor="#000000" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/StoreNumInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/WelcomeInfo" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/num" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Store Number" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/store_num" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enter the Store number" android:imeOptions="actionDone" android:password="true" android:textColor="#000000" > </EditText> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Options" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/PassInfo" android:orientation="horizontal" > <CheckBox android:id="@+id/signed_in" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Stay Signed-in" android:textColor="#000000" /> <Button android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Login" android:textSize="28dp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> </LinearLayout> 

    per lavorare in diverse size dello schermo ti suggerisco di creare diversi file xml in base alle size dello schermo …….. puoi guardarlo qui http://developer.android.com/guide/practices/screens_support.html . ..

    Provare questo codice XML, risolve il problema di youyr.

     <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:orientation="vertical" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/register_header" android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal" > <ImageView android:id="@+id/title_image" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:gravity="center" android:src="@drawable/ic_launcher" /> </RelativeLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/UserInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/user" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="User Name" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enter username" android:singleLine="true" android:textColor="#000000" > </EditText> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/PassInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/UserInfo" android:orientation="horizontal" > <TextView android:id="@+id/pwd" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Password" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/password" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enterpassword" android:singleLine="true" android:textColor="#000000" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/StoreNumInfo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@+id/WelcomeInfo" android:gravity="center_vertical" android:orientation="horizontal" > <TextView android:id="@+id/num" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Store Number" android:textColor="#000000" android:textSize="10pt" android:textStyle="bold" /> <EditText android:id="@+id/store_num" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:hint="Enter the Store number" android:imeOptions="actionDone" android:password="true" android:textColor="#000000" > </EditText> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Options" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/PassInfo" android:orientation="horizontal" > <CheckBox android:id="@+id/signed_in" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Stay Signed-in" android:textColor="#000000" /> <Button android:id="@+id/login" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="Login" android:textSize="28dp" android:textStyle="bold" /> </LinearLayout> </LinearLayout> </LinearLayout> </ScrollView> 

    Nella LinearLayout padre, aggiungi questo

     android:orientation="vertical" android:gravity="center" 

    .

    L'Android è un fan Android di Google, tutto su telefoni Android, Android Wear, Android Dev e applicazioni Android Games e così via.