2016-02-29 64 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;