如何在Android中使用尺寸支持不同的屏幕尺寸? [英] How to support different screen size in android using dimensions?

查看:131
本文介绍了如何在Android中使用尺寸支持不同的屏幕尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有40个xml布局的android应用.

I am developing an android app where i have 40 xml layouts.

以前的项目中,我使用多个文件夹来支持多个屏幕,但是现在我有40种布局,很难创建不同的文件夹.

Previous projects i was using multiple folders to support multiple screen but now i have 40 layouts its difficult to create different folder.

我听说过尺寸,但是我不知道如何实现.任何人都可以提供一些代码或示例

i heard about dimensions but i don't know how to achieve that .can any one provide some code or example

谢谢

在问这个问题之前,我先阅读以下答案,但在2013年被问到.

Before asking this question i read following answer but it is asked in 2013.

支持不同分辨率的android

推荐答案

如果布局中有十个Button和一个TextView,那么要做的第一件事就是将UI元素的值(在dp中)放入"res/values/dimens.xml"文件,例如:

If you have ten Buttons and one TextView in your layout, then the first thing to do would be to put the values (in dp) for the UI Elements in the 'res/values/dimens.xml' file, for example:

    <dimen name="btn_width">64dp</dimen>

要支持不同的屏幕尺寸或方向更改,您可能需要其他值.这些将进入相应的"values -.../dimen.xml"文件.

To support different screen sizes or orientation changes, you might want to have alternative values. These would go into the respective 'values-.../dimen.xml' file.

在布局xml文件中,您可以编写:

In the layout xml file, you could write:

 <Button
    android:layout_width="@dimen/btn_width"
    android:layout_height="@dimen/btn_height"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"/>

这篇关于如何在Android中使用尺寸支持不同的屏幕尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆