2014-10-31 187 views
1

我使用絕對佈局來劃分屏幕4種與圈中像這樣 enter image description hereAndroid的絕對佈局不同的屏幕尺寸

中心是做工精細的小屏幕尺寸,但大屏幕沒有關係「T。 如何用不同的屏幕寬度來做到這一點?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/linear" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<AbsoluteLayout 
    android:id="@+id/AbsoluteLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:l="true" 
    android:layout_centerInParent="true" 
    android:background="#fcf2cf" 
    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="com.example.organizer2.MainActivity" > 

    <Button 
     android:id="@+id/Button1" 
     android:layout_width="148dp" 
     android:layout_height="180dp" 
     android:layout_weight="1" 
     android:layout_x="0dp" 
     android:layout_y="0dp" 
     android:background="#219baa" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/Button2" 
     android:layout_width="148dp" 
     android:layout_height="180dp" 
     android:layout_weight="1" 
     android:layout_x="148dp" 
     android:layout_y="0dp" 
     android:background="#ef820b" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/Button3" 
     android:layout_width="148dp" 
     android:layout_height="180dp" 
     android:layout_weight="1" 
     android:layout_x="0dp" 
     android:layout_y="180dp" 
     android:background="#e3c800" 
     android:text="Button" /> 

    <Button 
     android:id="@+id/Button4" 
     android:layout_width="148dp" 
     android:layout_height="180dp" 
     android:layout_weight="1" 
     android:layout_x="148dp" 
     android:layout_y="180dp" 
     android:background="#36bc89" 
     android:text="Button" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_x="4dp" 
     android:layout_y="132dp" 
     android:src="@drawable/circle" /> 

    </AbsoluteLayout> 

</RelativeLayout> 
+0

AbsoluteLayout在API級別3上已棄用,不應使用。 您可以使用FrameLayout並使用重力來設置按鈕和圓圈位置。此外,您還需要爲Button製作自定義視圖以使它們成爲正方形。 – Gero 2014-10-31 20:50:25

回答

1

AbsolueLayouts是一個很大的禁忌,因爲它們不會對大小不同的設備做任何事情。嘗試使用LinearLayouts的組合來實現此視圖。通過將所有按鈕設置在linearlayout內部,並給每個按鈕權重1,我們告訴他們所有的增長,這絕對不會發生在絕對佈局中。線性佈局本身也是線性佈局,並且也被賦予了權重,所以它們將隨着有額外的屏幕空間佔用而增長(嵌套的LinearLayouts對於性能並不是很好,但現在不用擔心,因爲您似乎仍然正在學習基礎知識,並且您的應用的用戶不會注意到任何延遲)。 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/linear" 
android:layout_width="match_parent" 
android:background="#fcf2cf" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

    <LinearLayout 
    android:id="@+id/linear" 
    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" 
    android:orientation="vertical" > 

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/linear" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight=1 
      android:orientation="hoizontal" > 

       <Button 
       android:id="@+id/Button1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#219baa" 
       android:text="Button" /> 

       <Button 
       android:id="@+id/Button2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#ef820b" 
       android:text="Button" /> 

      </LinearLayout> 

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/linear" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight=1 
      android:orientation="hoizontal" > 

       <Button 
       android:id="@+id/Button3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#e3c800" 
       android:text="Button" /> 

       <Button 
       android:id="@+id/Button4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="#36bc89" 
       android:text="Button" /> 

      </LinearLayout> 
</LinearLayout> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:src="@drawable/circle" /> 

    </AbsoluteLayout> 

</RelativeLayout> 
+0

好吧,我正在使用linearlayout,但我canot把圓形放在中心像我的圖像 – jmt 2014-10-31 21:05:31

+1

對不起,我更新了我的答案,包括在圈子圖像android:layout_centerVertical =「true」 – 2014-10-31 21:07:59