2014-10-06 66 views
0

我如何製作應用程序以適合使用相對佈局的所有屏幕尺寸? 目前我使用的按鈕位置和尺寸的常數值,它只在HTC One S上看起來很好。這就是我用於開發的設備。 我希望按鈕位於每個顯示屏上的適當位置。如何製作應用以適合使用相對佈局的所有屏幕尺寸?

我在應用一個背景,那就是HUD,你可以看到它在這裏:

Head Up Display

按鈕和LED必須在孔內的確切位置。它看起來像這樣:

TRobotRC

佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/hud" 
android:orientation="horizontal" 
android:splitMotionEvents="false" 
android:windowEnableSplitTouch="false" 
tools:context=".MainActivity" > 

<ImageButton 
    android:id="@+id/connect_button" 
    android:layout_width="122sp" 
    android:layout_height="122sp" 
    android:layout_marginLeft="338dp" 
    android:layout_marginTop="54dp" 
    android:background="@drawable/connect_inactive" 
    android:contentDescription="@string/connect_button_description" /> 

<ImageButton 
    android:id="@+id/disconnect_button" 
    android:layout_width="122sp" 
    android:layout_height="122sp" 
    android:layout_marginLeft="338dp" 
    android:layout_marginTop="195dp" 
    android:background="@drawable/disconnect_inactive" 
    android:contentDescription="@string/disconnect_button_description" /> 

<ImageButton 
    android:id="@+id/throttleup_button" 
    android:layout_width="122sp" 
    android:layout_height="122sp" 
    android:layout_marginLeft="483dp" 
    android:layout_marginTop="54dp" 
    android:background="@drawable/throttleup_inactive" 
    android:contentDescription="@string/throttleup_button_description" /> 

<ImageButton 
    android:id="@+id/throttledown_button" 
    android:layout_width="122sp" 
    android:layout_height="122sp" 
    android:layout_marginLeft="483dp" 
    android:layout_marginTop="195dp" 
    android:background="@drawable/throttledown_inactive" 
    android:contentDescription="@string/throttledown_button_description" /> 

<ImageButton 
    android:id="@+id/moveforward_button" 
    android:layout_width="170sp" 
    android:layout_height="160sp" 
    android:layout_marginLeft="79dp" 
    android:layout_marginTop="30dp" 
    android:background="@drawable/control_forward_inactive" 
    android:contentDescription="@string/moveforward_button_description" /> 

<ImageButton 
    android:id="@+id/movebackward_button" 
    android:layout_width="170sp" 
    android:layout_height="160sp" 
    android:layout_marginLeft="79dp" 
    android:layout_marginTop="153dp" 
    android:background="@drawable/control_backward_inactive" 
    android:contentDescription="@string/movebackward_button_description" /> 

<ImageButton 
    android:id="@+id/moveleft_button" 
    android:layout_width="170sp" 
    android:layout_height="160sp" 
    android:layout_marginLeft="17dp" 
    android:layout_marginTop="92dp" 
    android:background="@drawable/control_left_inactive" 
    android:contentDescription="@string/moveleft_button_description" /> 

<ImageButton 
    android:id="@+id/moveright_button" 
    android:layout_width="170sp" 
    android:layout_height="160sp" 
    android:layout_marginLeft="141dp" 
    android:layout_marginTop="92dp" 
    android:background="@drawable/control_right_inactive" 
    android:contentDescription="@string/moveright_button_description" /> 

<ImageView 
    android:id="@+id/led" 
    android:layout_width="45sp" 
    android:layout_height="45sp" 
    android:layout_marginLeft="297dp" 
    android:layout_marginTop="280dp" 
    android:background="@drawable/led_inactive" 
    android:contentDescription="@string/led_description" /> 

</RelativeLayout> 

回答

2

而不是硬編碼值,使用dimen文件用於這一目的。 爲值指定不同的文件夾,如values-large等,並根據屏幕尺寸創建尺寸文件並放置特定值。

對所有Layouts使用加權,而不是給定常數值,因爲weight是屏幕上的一種類型,默認情況下支持屏幕顯示爲%。

這樣

layout-smalllayout-largelayout-xlarge所以ü複製您的佈局在這些文件夾中創建多個屏幕尺寸佈局的文件夾XML文件,現在ü可以編輯框指定的寬度和大小。

這些鏈接將幫助您爲多屏幕創建應用程序:

  1. http://developer.android.com/training/multiscreen/screensizes.html

  2. http://developer.android.com/guide/practices/screens_support.html

這些都是Android的官方文件上實現多種屏幕尺寸的支持。

http://developer.android.com/guide/practices/screens_support.html

相關問題