简体   繁体   English

使Android活动适合所有屏幕尺寸

[英]Make android activity fit all screen sizes

This is the XML file for my activity. 这是我的活动的XML文件。

How do I make this fit all screen sizes without the image button getting messed up! 如何使它适合所有屏幕尺寸,而不会弄乱图像按钮!

I am using a relative-layout , should I use a linear-layout instead? 我正在使用相对布局 ,应该使用线性布局吗?

Following is my code. 以下是我的代码。

<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" />

I think you have to create each object and add into layout programatically, and using ratio for each object. 我认为您必须创建每个对象并以编程方式添加到布局中,并对每个对象使用比率。 For example: ratio of your screen is 9/16. 例如:屏幕的比例为9/16。

height = size.y; 高度= size.y; width = ratio * height; 宽度=比率*高度;

Every object will set layout param or margin param, they should follow this width and height. 每个对象都将设置布局参数或边距参数,它们应遵循此宽度和高度。

For example with one of your button: 例如,使用您的一个按钮:

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);

You have to write xml code for large screen. 您必须为大屏幕编写xml代码。 I think you are keeping your xml file in res/layout folder. 我认为您将xml文件保留在res/layout文件夹中。 if it is you can keep three folders to differentiate various screens like res/layout-small , res/layout , res/layout-long . 如果是这样,您可以保留三个文件夹来区分各种屏幕,例如res/layout-smallres/layoutres/layout-long In these three folders you can set different alignements to various screen types. 在这三个文件夹中,可以为各种屏幕类型设置不同的对齐方式。 For Medium Screen, keep your xml code in layout, and for small screens - keep it in layout-small and etc., and set in your AndroidManifest.xml ... 对于中型屏幕,将您的xml代码保留在布局中,对于小屏幕-将其保留在layout-small等中,并在您的AndroidManifest.xml设置...

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

don't use relative layout. 不要使用相对布局。 use Linear layout with weightSum . 使用带有weightSum线性布局。

its work in most of device. 它可以在大多数设备上工作。 for tablets you have to re design new layouts in layout-sw600dp, layout-sw720dp folder. 对于平板电脑,您必须在layout-sw600dp,layout-sw720dp文件夹中重新设计新的布局。

sample for weightSum 重量样本

here is guidelines for design multiple screen size 这是设计多种屏幕尺寸的准则

more Extra detils here is Google I/O App download code and check it. 更多Extra detils是Google I / O App下载代码,并进行检查。

I prefer getting the screen size and then doing all margins and sizes programmatically in code. 我更喜欢先获取屏幕尺寸,然后以编程方式在代码中进行所有边距和尺寸设置。 More work, but way more control. 工作更多,但控制方式更多。
Just an example, works this way with everything you have, textsize, viewsize, positions...: 只是一个例子,这种方式适用于您拥有的所有内容,文本大小,视图大小,位置...:

    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);

The best and easiest way to do that is to right click the res folder in android studio and press show in explorer 最好的和最简单的方法是右键单击android studio中的res文件夹,然后在资源管理器中按show

Make four different folders in the res with these names : 在res中使用以下名称创建四个不同的文件夹:

layout-small

,

layout-normal

,

layout-large

,

layout-xlarge

Note : after done this and make sure your xml is in the four folders with exact folder names and modified according to your preference 注意:完成此操作后,请确保您的xml位于具有正确文件夹名称的四个文件夹中,并根据您的喜好进行了修改

Delete the layout and leave the other four 删除layout并保留其他四个

and put your xml in the four then modify it on each according to the size 并将您的xml放在四个中,然后根据大小对每个xml进行修改

now finally in your AndroidManifest add the following 现在终于在您的AndroidManifest添加以下内容

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM