2014-02-22 128 views
1

這是我的活動的XML文件。使Android活動適合所有屏幕尺寸

如何讓這適合所有屏幕尺寸,而不會出現圖像按鈕變得混亂!

我正在使用相對佈局,我應該使用線性佈局而不是?

以下是我的代碼。

<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" 
android:background="@drawable/good"> 

<ImageButton 
    android:id="@+id/ImageButton03" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/imageButton1" 
    android:layout_alignTop="@+id/ImageButton01" 
    android:background="@drawable/btn_blue" 
    android:src="@drawable/plus" 
    android:onClick="NewCategory"/> 

<ImageButton 
    android:id="@+id/ImageButton01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/ImageButton02" 
    android:layout_below="@+id/ImageButton02" 
    android:layout_marginTop="29dp" 
    android:background="@drawable/btn_blue" 
    android:src="@drawable/plus" 
    android:onClick="NewItem" /> 

<ImageButton 
    android:id="@+id/ImageButton04" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/ImageButton01" 
    android:layout_alignLeft="@+id/ImageButton03" 
    android:background="@drawable/btn_blue" 
    android:src="@drawable/call" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:text="سـوبر ماركت التنـور" 
    android:textSize="40dp" 
    android:background="@drawable/btn_red" 
    android:textColor="@color/white" /> 

<ImageButton 
    android:id="@+id/imageButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/button1" 
    android:layout_below="@+id/button1" 
    android:layout_marginTop="24dp" 
    android:background="@drawable/btn_blue" 
    android:onClick="ViewAllCategories" 
    android:src="@drawable/cart" /> 

<ImageButton 
    android:id="@+id/ImageButton05" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/ImageButton02" 
    android:layout_alignLeft="@+id/button1" 
    android:background="@drawable/btn_blue" 
    android:onClick="CreateNewUserAccount" 
    android:src="@drawable/user" /> 

<ImageButton 
    android:id="@+id/ImageButton02" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/ImageButton05" 
    android:layout_below="@+id/imageButton1" 
    android:layout_marginTop="33dp" 
    android:background="@drawable/btn_blue" 
    android:onClick="joinfacebook" 
    android:src="@drawable/f" /> 

+0

這取決於你正在尋找的輸出。你可以爲你想要的最終屏幕放置一個屏幕截圖。 –

+0

如果這些都來自.xml。你不關閉RelativeLayout! – Umitk

+0

定義「搞砸」。 – davidcesarino

回答

0

你必須寫大屏幕xml代碼。我認爲你將xml文件保存在res/layout文件夾中。如果是這樣,你可以保留三個文件夾來區分各種屏幕,如res/layout-small,res/layoutres/layout-long。在這三個文件夾中,您可以爲各種屏幕類型設置不同的對齊方式。對於中等屏幕,讓您的xml代碼佈局,爲小屏幕 - 保持它在layout-small等,並在AndroidManifest.xml設置...

<supports-screens 
    android:smallScreens="true" 
    android:normalScreens="true" 
    android:largeScreens="true"   
    android:anyDensity="true" /> 
0

我更喜歡獲得屏幕大小,然後在代碼中以編程方式執行所有邊距和大小。更多的工作,但方式更多的控制。
只是一個例子,這種方式工作與你所擁有的一切,TEXTSIZE,viewsize,位置...:

DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics(); 
    int displayHeight = metrics.heightPixels; 
    int displayWidth = metrics.widthPixels; 
    float scaledDensity = metrics.scaledDensity; 

    float percentageToMoveViewDown = (float) 20.0; 
    float viewY_float = (float) ((displayHeight/100.0) * percentageToMoveViewDown); 
    int viewY_int = Math.round(viewY_float); 

    RelativeLayout.LayoutParams view_Layout_params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    view_Layout_params.topMargin = viewY_int; 
    view.setLayoutParams(view_Layout_params); 
1

我認爲你必須創建每個對象,並添加到佈局編程,並使用比爲每個對象。例如:你的屏幕比例是9/16。

height = size.y; width = ratio * height;

每個對象都會設置佈局參數或邊距參數,它們應該遵循這個寬度和高度。

例如你的按鈕中的一個:

imageButton1 = new ImageView(this); 
imageButton1.setBackgroundResource(R.drawable.abc); 
imageButton1.setLayoutParams(new LayoutParams(width/100, height/100)); 
marginParams = new ViewGroup.MarginLayoutParams(imageButton1.getLayoutParams()); 
marginParams.setMargins(width/24, height/12, 0, 0); 
layoutParams = new RelativeLayout.LayoutParams(marginParams); 
imageButton1.setLayoutParams(layoutParams); 
layout.addView(imageButton1); 
0

要做到這一點是右擊res文件夾在Android Studio中,按顯示在資源管理器

讓四個不同的文件夾中的最好和最簡單的方法在這些名字中的RE:

layout-small 

layout-normal 

layout-large 

layout-xlarge 

注:後做到了這一點,並確保你的XML與確切的文件夾名稱的四個文件夾,並修改根據自己的喜好

刪除layout並離開其他四個

並把你的xm l在四個再根據大小

現在終於在修改它在每個AndroidManifest添加以下

<supports-screens 
    android:resizeable="true" 
android:smallScreens="true" 
android:normalScreens="true" 
android:largeScreens="true" 
android:xlargeScreens="true" />