2017-07-25 52 views
-3

我是一個Android應用程序開發的初學者,我有一個問題,我找不到任何解決方案。我在Nexus 5個應用程序和它的作品完美,佈局之間沒有空的空間,但是當我切換到的Nexus 4,這是發生了什麼(我用的顏色佈局分開):Android - 計算器支持所有屏幕尺寸

https://scontent.fath3-1.fna.fbcdn.net/v/t34.0-12/20403684_1379424475444386_454511879_n.jpg?oh=440d7b9ab5670c8cad87133475ec0cb6&oe=5979CDED


如果我切換到像素XL:

https://scontent.fath3-1.fna.fbcdn.net/v/t34.0-12/20403737_1379426118777555_859609190_n.jpg?oh=61ccd01da7a4470491fb4125da8db43c&oe=597996E4

我能做些什麼,所以也沒有對權利的任何空的空間(下面有以防萬一貼你需要它的XML代碼) 。

<LinearLayout 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:background="@android:color/black" 
android:orientation="vertical" 
android:weightSum="1"> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#f00"> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:onClick="onClickDTB" 
      android:text="Convert to Binary" 
      android:textAlignment="center" 
      android:textAllCaps="true" 
      android:textSize="14sp" 
      android:textStyle="bold|italic" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#0f0"> 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="match_parent" 
      android:layout_height="98dp" 
      android:layout_weight="0.01" 
      android:fontFamily="serif-monospace" 
      android:text="Calculator" 
      android:textAlignment="textEnd" 
      android:textColor="@android:color/white" 
      android:textSize="35dp" 
      android:textStyle="bold" /> 
    </LinearLayout> 


<LinearLayout 


    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="#f0f" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="72.4dp" 
     android:layout_height="match_parent" 
     android:background="#FFA500" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="72.4dp" 
     android:layout_height="match_parent" 
     android:background="#000FFF" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="72.4dp" 
     android:layout_height="match_parent" 
     android:background="#8B0000" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="72.4dp" 
     android:layout_height="match_parent" 
     android:background="#800080" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="72.4dp" 
     android:layout_height="match_parent" 
     android:background="#2E8B57" 
     android:orientation="vertical"> 

    </LinearLayout> 


</LinearLayout> 

+0

https://developer.android.com/ guide/practices/screens_support.html https://developer.android.com/training/multiscreen/screensizes.html – Nenco

+0

使用重量和重量總和屬性爲你佈局 –

+0

我見過每一個可用的鏈接,我試過了,但它似乎不工作。這就是爲什麼我問我需要某人來解釋基於我的代碼的問題,所以我明白一般會發生什麼。 –

回答

0

必須使用以及重總和這樣

<LinearLayout 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:background="@android:color/black" 
    android:orientation="vertical" 
    android:weightSum="1"> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#f00"> 

     <Button 
      android:id="@+id/button" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:onClick="onClickDTB" 
      android:text="Convert to Binary" 
      android:textAlignment="center" 
      android:textAllCaps="true" 
      android:textSize="14sp" 
      android:textStyle="bold|italic" /> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#0f0"> 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="match_parent" 
      android:layout_height="98dp" 
      android:layout_weight="0.01" 
      android:fontFamily="serif-monospace" 
      android:text="Calculator" 
      android:textAlignment="textEnd" 
      android:textColor="@android:color/white" 
      android:textSize="35dp" 
      android:textStyle="bold" /> 
    </LinearLayout> 


    <LinearLayout 


     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#f0f" 
     android:weightSum="100" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="20" 
      android:layout_height="match_parent" 
      android:background="#FFA500" 
      android:orientation="vertical"> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="20" 
      android:layout_height="match_parent" 
      android:background="#000FFF" 
      android:orientation="vertical"> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="20" 
      android:background="#8B0000" 
      android:orientation="vertical"> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_weight="20" 
      android:layout_height="match_parent" 
      android:background="#800080" 
      android:orientation="vertical"> 

     </LinearLayout> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="20" 
      android:background="#2E8B57" 
      android:orientation="vertical"> 

     </LinearLayout> 


    </LinearLayout> 
    </LinearLayout> 
+0

哦,我看到...相同的答案,這意味着它是做到這一點的方法 –

+0

是的佈局重量會將您的家長佈局分爲5個部分,完全覆蓋您的屏幕,沒有剩下右側空間。 –

+0

非常感謝你們,它實際上是在工作。 –

0

試試下面的代碼

<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#f00"> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:onClick="onClickDTB" 
     android:text="Convert to Binary" 
     android:textAlignment="center" 
     android:textAllCaps="true" 
     android:textSize="14sp" 
     android:textStyle="bold|italic" /> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#0f0"> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="match_parent" 
     android:layout_height="98dp" 
     android:layout_weight="0.01" 
     android:fontFamily="serif-monospace" 
     android:text="Calculator" 
     android:textAlignment="textEnd" 
     android:textColor="@android:color/white" 
     android:textSize="35dp" 
     android:textStyle="bold" /> 
</LinearLayout> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#f0f" 
    android:orientation="horizontal"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#FFA500" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#000FFF" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#8B0000" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#800080" 
     android:orientation="vertical"> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#2E8B57" 
     android:orientation="vertical"> 

    </LinearLayout> 


</LinearLayout> 
+0

所以你實際添加layout_weight到所有的佈局? –

+0

是@ A.Tressos,如果需要適合所有屏幕尺寸,則需要添加。 –