在不同设备Android Studio

时间:2016-02-29 18:23:00

标签: android android-layout android-studio

我正在Android Studio中制作一个Tic Tac Toe应用程序以获得乐趣并且有问题。如果我在布局编辑器中使用nexus5x进行预览我的应用程序是完美的。如果我将其更改为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"/>

3 个答案:

答案 0 :(得分:1)

当我看到你的图像时,它们收到了错误的边距,即较小的屏幕接收到适合nexus 5的较大边距。因为你已经为每个屏幕尺寸硬编码了边距,这是不推荐的。尝试将您的边距放在dimension.xml中,然后在较小的屏幕上创建另一个边距。

使用DP的全部意义在于,您不必担心这一点。设备的边距大致相同,但如果您依赖于在一个特定的设备分辨率/密度组合上进行排序,那么当您在其他设备上进行测试时,您肯定会感到惊讶。

也就是说,如果你确实需要为不同的屏幕尺寸指定不同的边距,只需在res / values中添加一个XML文件 - 就像dimens.xml:

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

然后为您需要的每个特定设备限定符添加其中一个XML(例如,value-large,values-sw600dp,values-xlarge等),并根据需要修改该值。如果要在布局中使用这些尺寸,只需使用:

android:layout_margin="@dimen/my_view_margin"

Android会为正在运行的设备选择正确的值。

答案 1 :(得分:0)

根据{{​​3}}

,Nexus 4的宽度为384dp,Nexus 5X的宽度为411dp。

当我必须使用网格布局制作应用时,我this.每个项目的尺寸,并为间距添加了一些填充。

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

int itemWidth = width / 3;

答案 2 :(得分:0)

也许Percent Support Library可以帮助你。

它允许您将LayoutParams设置为百分比而不是硬值。

相关问题