How can I have android "scale" my application?
I am creating my first android application and this has been driving me insane.
My application is for the android smartphones, and I only need one specific design for my layout. I do not need to change the design of the layout for each device. All I want to do is resize the layout to support all the different screen sizes and resolutions. I have tried multiple things and can't seem to get it right.
For Example: I have several buttons on my pages, and the buttons need to be at a specific point on the image that I have as the background. These buttons need to still be at the same point on the image even if the device's screen size was increased or the screen resolution was increased.
Just some info about my application:
-all my button locations are in DIP (one of the solutions that I tried had me do this) -my layout XML files are in a folder titled "layout"
Any help will be greatly appreciated
Here is the code for one of my layouts:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/mybackground"
>
<Button
android:layout_width="115dip"
android:layout_marginTop="450dip"
android:layout_marginLeft="105dip"
android:layout_height="50dip"
android:id="@+id/button01"
>
</Button>
<Butto开发者_如何学Gon
android:layout_width="70dip"
android:layout_marginTop="460dip"
android:layout_marginLeft="250dip"
android:layout_height="40dip"
android:id="@+id/button03"
>
</Button>
</RelativeLayout>
The code above is one of my layouts. This layout has two buttons that need to be in the same location in relation to the background "mybackground". These buttons have the transparent attribute applied to them so that elements on the image are the "buttons". This works on my Droid2, but then when applied to a screen bigger or more dense, the buttons are no longer in the correct positions in relation to the image.
What adamp is saying in his comment on Rollin_s's question is: What is the reason for you needing the buttons to be over a specific spot on the background? If that spot is another image (for example) that needs to be aligned, make that image its own ImageView and align the button with RelativeLayout parameters. Maybe if you included a screenshot, someone could give you a better example of what to do. Also, GridView and TableLayout are good objects for aligning layout components to fit different screen sizes.
Try Relative Layout:
http://developer.android.com/reference/android/widget/RelativeLayout.html
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
Sounds a bit funky but if all youve got to work with is a flattened image of the equivalent iPhone UI and the only UI elements you need to add are buttons over top of it, I might be tempted to try the following alternative to the tedious work of trying to get the transparent buttons to align:
- Determine where your button locations are relative to your normal/unscaled image, record the top, left, bottom, right values in some data structure of your choosing
- Place your image in an ImageView set to scale using FitXY
- calculate the scaling that android has applied to your image by examining the ImageViews H & W and comparing to what your native image H & W were
- do some math to scale the top,left,bottom, and right values from your original values
- Then in an onClick handler for your image view you should be able to test to see if the touched coordinates are within any of the scaled boundaries you've now got.
Probably would be more accurate, easier to maintain, and less hassle than fiddling with the layout stuff.
Of course, the more correct solution would be to get the actual individual graphic elements used to do the iPhone app and re-implement the UI in android proper.
Good luck.
精彩评论