I made a calculator app for Android and used an Absolute layout to position the buttons and 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. 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 phone开发者_如何学Cs 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.
To place elements side-by-side you can use LinearLayout
with orientation="horizontal"
.
To deal with scaling try to use layout_weight
parameter. I.e. 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.
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,
精彩评论