2014-02-22 60 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" />