简体   繁体   English

Android:关于大小/分辨率独立应用程序的问题?

[英]Android: Question about size/resolution independent applications?

I apologize in advance for asking this question, I know similar questions have already been asked hundreds of times, but despite the fact that I read the Android Screen Support guide several times, I still don't understand how to create a basic layout that fits several screen without being able to use proportional dimensions.我提前为提出这个问题道歉,我知道类似的问题已经被问了数百次,但是尽管我多次阅读Android 屏幕支持指南,但我仍然不明白如何创建适合的基本布局几个屏幕无法使用比例尺寸。

So basically, if I sum-up what this guide tells us to do:所以基本上,如果我总结一下本指南告诉我们要做的事情:

  • We should create several layout resources for each "size group" and "density group" of devices you want your app to be compatible with.我们应该为您希望应用程序兼容的每个“大小组”和“密度组”设备创建多个布局资源。
  • We should use RelativeLayout or FrameLayout instead of AbsoluteLayout我们应该使用RelativeLayoutFrameLayout而不是AbsoluteLayout
  • We should use dp dimensions instead of px dimensions to get rid of the density difference problem.我们应该使用dp尺寸而不是px尺寸来摆脱密度差异问题。

Ok.好的。 This makes sense.这是有道理的。

Now, here are my questions (I apologize in advance for their silliness):现在,这是我的问题(我提前为他们的愚蠢道歉):

  • Why do I have to create different layout resources for different density groups if I use Density Independent Pixels (dp) dimensions?如果我使用与密度无关的Density Independent Pixels (dp)尺寸,为什么我必须为不同的density groups创建不同的布局资源?
  • I suppose that the point of having different sets of resources for different screen sizes is that you might want your application layout to look different on a small and a large device, not to have the exact same layout with different dimensions, correct?我想为不同的屏幕尺寸提供不同的资源集的意义在于,您可能希望您的应用程序布局在小型和大型设备上看起来不同,而不是具有不同尺寸的完全相同的布局,对吗? So basically it means that if I just want an application that looks exactly the same on all devices (just shrinking/expanding to fit the screen size), I only have to define one set of resources, correct?所以基本上这意味着如果我只想要一个在所有设备上看起来完全相同的应用程序(只是缩小/扩大以适应屏幕尺寸),我只需要定义一组资源,对吗?
  • If I want to create a really simple layout, that just contains two buttons, where each buttons take 50% of the screen's width, how do I do that by just using dp dimensions?如果我想创建一个非常简单的布局,它只包含两个按钮,每个按钮占据屏幕宽度的 50%,我该如何通过仅使用dp尺寸来做到这一点?

Thank you, and sorry again for going over this topic again...谢谢你,再次抱歉再次讨论这个话题......

You do not have to create different layouts.您不必创建不同的布局。 I mostly only use one layout for portrait and one for landscape mode leaving everything else to the system.我大多只使用一种布局用于纵向模式,一种用于横向模式,将其他所有内容留给系统。

If you want to get 2 buttons of the same size just use如果您想获得 2 个相同大小的按钮,只需使用

android:layout_width="fill_parent"
android:layout_weight="1"

for both buttons and put them into a linear layout container.两个按钮并将它们放入线性布局容器中。

edit (complete code, will give two buttons side by side):编辑(完整的代码,将并排给出两个按钮):

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:orientation="horizontal">
    <Button 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:text="@string/b1" android:onClick="btn1" />
    <Button 
        android:layout_width="fill_parent" android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:text="@string/b2" android:onClick="btn2" />
</LinearLayout>

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

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