[英]Make android activity fit all screen sizes
这是我的活动的XML文件。
如何使它适合所有屏幕尺寸,而不会弄乱图像按钮!
我正在使用相对布局 ,应该使用线性布局吗?
以下是我的代码。
<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" />
我认为您必须创建每个对象并以编程方式添加到布局中,并对每个对象使用比率。 例如:屏幕的比例为9/16。
高度= size.y; 宽度=比率*高度;
每个对象都将设置布局参数或边距参数,它们应遵循此宽度和高度。
例如,使用您的一个按钮:
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);
您必须为大屏幕编写xml
代码。 我认为您将xml
文件保留在res/layout
文件夹中。 如果是这样,您可以保留三个文件夹来区分各种屏幕,例如res/layout-small
, res/layout
, res/layout-long
。 在这三个文件夹中,可以为各种屏幕类型设置不同的对齐方式。 对于中型屏幕,将您的xml
代码保留在布局中,对于小屏幕-将其保留在layout-small
等中,并在您的AndroidManifest.xml
设置...
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
不要使用相对布局。 使用带有weightSum
线性布局。
它可以在大多数设备上工作。 对于平板电脑,您必须在layout-sw600dp,layout-sw720dp文件夹中重新设计新的布局。
更多Extra detils是Google I / O App下载代码,并进行检查。
我更喜欢先获取屏幕尺寸,然后以编程方式在代码中进行所有边距和尺寸设置。 工作更多,但控制方式更多。
只是一个例子,这种方式适用于您拥有的所有内容,文本大小,视图大小,位置...:
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);
最好的和最简单的方法是右键单击android studio中的res文件夹,然后在资源管理器中按show
在res中使用以下名称创建四个不同的文件夹:
layout-small
,
layout-normal
,
layout-large
,
layout-xlarge
注意:完成此操作后,请确保您的xml位于具有正确文件夹名称的四个文件夹中,并根据您的喜好进行了修改
删除layout
并保留其他四个
并将您的xml放在四个中,然后根据大小对每个xml进行修改
现在终于在您的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.