简体   繁体   English

Android绝对布局在某些手机上看起来不错,但在其他手机上却不行吗?

[英]Android Absolute Layout looks good on some phones but not others?

I made a calculator app for Android and used an Absolute layout to position the buttons and textview. 我为Android开发了一个计算器应用程序,并使用“绝对”布局来放置按钮和textview。 It looks good on my dad's HTC Hero, but when I put it on my EVO everything is compressed to the upper left. 在我父亲的HTC Hero上看起来不错,但是当我将其放在EVO上时,所有内容都会压缩到左上方。 I think it's because my screen resolution is bigger than my dad's, so the pixel measurements I use in my absolute layout do not scale correctly on my phone like it does on my dads because he has fewer pixels. 我认为这是因为我的屏幕分辨率大于我父亲的屏幕分辨率,所以我在绝对布局中使用的像素尺寸无法像我父亲那样在手机上正确缩放,因为他的像素较少。

I think if I use a different layout like linear layout it will scale correctly on all phones of different resolutions. 我认为,如果我使用线性布局等其他布局,它将在所有分辨率不同的手机上正确缩放。 What layout can I use and how can I position the buttons where I want with it? 我可以使用哪种布局,如何将按钮放置在需要的位置? With linear layout everything just stacks on top of each other and I cant figure out how to put things side by side and all over like buttons on a calculator app should be. 使用线性布局时,所有内容都相互堆叠,我无法弄清楚如何像计算器应用程序上的按钮那样并排放置所有内容。 Absolute layout was the only way I could think. 绝对布局是我能想到的唯一方法。 Can someone maybe give me an example or show me a layout of a calculator app you made so I can see how you did it? 有人可以给我一个例子,或告诉我您制作的计算器应用程序的布局,以便我可以看到您如何做的吗?

AbsoluteLayout is not recommended. 不建议使用AbsoluteLayout

To place elements side-by-side you can use LinearLayout with orientation="horizontal" . 要并排放置元素,可以将LinearLayoutorientation="horizontal"

To deal with scaling try to use layout_weight parameter. 要处理缩放,请尝试使用layout_weight参数。 Ie if you want to create a two button side-by-side taking a full space in width, and each button half of a total width you can do the following 即,如果您要并排创建两个按钮,并在整个宽度上占用整个空间,并且每个按钮占总宽度的一半,则可以执行以下操作

<LinearLayout
    android:orientation="horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent">
    <Button 
         android:layout_width="0dp"    
         android:layout_height="wrap_content"  
         android:layout_weight="1" />   
    <Button 
         android:layout_width="0dp"    
         android:layout_height="wrap_content"  
         android:layout_weight="1" />
</LinearLayout>

Also, consider using RelativeLayout where you can place each element relative to previously placed ones. 另外,考虑使用RelativeLayout ,您可以在其中放置每个相对于先前放置的元素的元素。

STOP USING ABSOLUTE LAYOUT!!!!! 停止使用绝对布局!!!!

Prefer Relative or Nested Linear Layout to handle situation. 首选相对或嵌套线性布局来处理情况。 Use absolute layout only when its exactly required on a specific devices, until and unless don't go for it. 仅当在特定设备上确实需要绝对布局时才使用绝对布局,直到并且除非不要这么做为止。

Thanks, 谢谢,

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

相关问题 为什么 Android 应用在某些手机上看起来不一样 - Why Android app looks different in some phones Android:屏幕布局在相同型号的2部手机上看起来有所不同 - Android: Screen Layout looks different on 2 phones of the same model 在联系人上设置 custom_ringtone 适用于某些 android 手机,而不适用于其他手机 - Setting custom_ringtone on contact works on some android phones and not others android - 布局在某些设备中看起来很混乱 - android - layout looks messed up in some devices 在布局编辑器中布局看起来很好 - 完全不在仿真设备上 - layout looks good in android layout editor - completely off on emulated devices 在某些手机上,Android视图到布局两侧的距离不同 - Android view is not the same distance to both sides of the layout on some phones 手机的Android Studio布局 - Android Studio layout for phones 为什么OpenGL在某些手机上运行而不在其他手机上运行? - Why does OpenGL run on some phones but not on others? 在某些电话上出现NullPointerException,而不是其他电话(相同的API级别) - NullPointerException on some phones, not others (same API level) 手机与平板电脑在Android上的布局 - Layout on Phones vs Tablets on android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM