布局看起来不同?

时间:2015-11-29 04:55:06

标签: android arrays string android-layout android-studio

我知道不同屏幕尺寸的布局可能会有所不同,但在制作此布局时我并没有考虑到这一点。因此,它默认设计为正常屏幕尺寸,我的手机屏幕也正常。

问题是,布局看起来应该是不同的。以下是我在手机上的操作方式,请注意四个图像按钮位于右侧

enter image description here

这应该是什么样的,如android studio布局工具所示。 注意,它很好地居中,因为它应该是

enter image description here

这是我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<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.ruchir.circleswithmap.MainActivity"
    android:id="@+id/layout">

    <ImageButton
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/Blue"
        android:layout_marginTop="108dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="73dp"
        android:layout_marginStart="73dp"
        android:background="@drawable/bluecircle" />

    <ImageButton
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/Green"
        android:layout_below="@+id/Blue"
        android:layout_alignLeft="@+id/Blue"
        android:layout_alignStart="@+id/Blue"
        android:background="@drawable/greencircle" />

    <ImageButton
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/Red"
        android:layout_alignTop="@+id/Blue"
        android:layout_toRightOf="@+id/Blue"
        android:layout_toEndOf="@+id/Blue"
        android:background="@drawable/redcircle" />

    <ImageButton
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/Purple"
        android:layout_alignBottom="@+id/Green"
        android:layout_alignLeft="@+id/Red"
        android:layout_alignStart="@+id/Red"
        android:background="@drawable/purplesquare" />

</RelativeLayout>

这是为什么?我该如何修复它,以便手机上的布局也居中,应该是这样?非常感谢你的帮助!

1 个答案:

答案 0 :(得分:1)

添加

android:gravity="center"

到RelativeLayout以使布局内容居中并删除

android:layout_marginTop="108dp"
android:layout_marginLeft="73dp"
android:layout_marginStart="73dp"

来自第一个ImageButton,如果你想水平和垂直居中。