2016-02-29 42 views
1

我在做Android Studio中一個有趣的井字應用程序,有一個問題。如果我nexus5x使用預覽在佈局編輯器我的應用程序是面向perfect.If我改變這nexus4或一個平板電腦的方向是搞砸了。 這是我的意思的圖片。 Nexus 5x as previewNexus 4 as preview級圖像工作室

從我的XML代碼的示例是如下:

<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:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.mpampis.tictactoe2.MainActivity"> 

<GridLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_centerVertical="true" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/board" 
    android:columnCount="3" 
    android:rowCount="4"> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView3" 
     android:layout_row="0" 
     android:layout_column="2" 
     android:src="@drawable/kokkinov3" 
     android:layout_marginTop="25dp" 
     android:layout_marginLeft="20dp"/> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView2" 
     android:layout_row="0" 
     android:layout_column="1" 
     android:src="@drawable/kokkinov3" 
     android:layout_marginTop="25dp" 
     android:layout_marginLeft="30dp"/> 
+0

您是否正在做任何事情來縮放您的圖像以適應運行時在網格中可用的空間量? –

+0

我在drawables中有一個.png文件,當我在GridLayout中使用它時,我將它放在邊緣的位置來定位它。 – oMpamparos

+0

有時您需要爲每個res創建xml。 RES /佈局/ main_activity.xml#針對手機(小於600dp的可用寬度) RES /佈局sw600dp/main_activity.xml#針對7」 片劑(寬度爲600 dp和更大) RES /佈局sw720dp/main_activity.xml#針對10「平板電腦(720dp寬和更大) - http://developer.android.com/guide/practices/screens_support.html –

回答

1

當我看到你的圖片,他們正在接受錯誤的利潤率,即小屏幕接收較大的利潤率已硬編碼的利潤爲每個屏幕尺寸,這是不建議其適於關係5。嘗試將尺寸放在dimension.xml中,併爲較小的屏幕製作另一個尺寸。

那麼使用DP的重點就是讓您不必擔心這一點。在不同設備上,邊距將大致相同,但如果您依靠某種設備的分辨率/密度組合進行排列,則當您在其他設備上進行測試時,您一定會驚喜不已。

這就是說,如果你確實需要針對不同的屏幕尺寸指定不同的利潤率,只需在res /值添加一個XML文件 - 像dimens.xml:

<resources 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    > 
    <dimen name="my_view_margin">10dip</dimen> 
</resources> 

然後將這些個XML的一個對於您需要的每個特定設備限定符(例如,values-large,values-sw600dp,values-xlarge等)並按照您認爲合適的方式修改該值。當你想在佈局中使用這些尺寸,只需使用:

android:layout_margin="@dimen/my_view_margin" 

和Android將挑選任何設備恰好是其上運行的正確值。

+0

用這種方法,我應該爲每個xh xxh等設備設置一個邊距。 – oMpamparos

+0

@oMpamparos檢查更新的答案 – UMESH0492

+0

Thnx你dude ...你有任何想法如何爲不同的值製作XML文件嗎?像大屏幕xml,小屏幕xml等 – oMpamparos

0

的Nexus 4是384dp寬和Nexus 5X是411dp寬,根據this.

當我不得不作出應用程序與網格佈局,我manually calculated每個項目的尺寸,並添加了一些填充間距。

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 
display.getSize(size); 
int width = size.x; 
int height = size.y; 

int itemWidth = width/3;