Android Studio中的预览与模拟器

时间:2015-11-26 17:30:34

标签: android xml android-layout layout android-studio

我是Android编程的新手,我已经面临着一个人们可以想象的最简单的问题:在屏幕上放置一个按钮。在activity.xml文件的预览中,一切都很好但是在模拟器中它搞砸了,按钮和标签不在它们应该的位置。

以下是一个示例:我在浮动操作按钮的左侧放置了一个按钮,但在模拟器中它位于其上方。

Android Studio预览:

Android Studio Preview

仿真器:

Emulator

以下是XML代码:

activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.ed.wtf.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>



content.xml

        <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.ed.wtf.MainActivity"
        tools:showIn="@layout/activity_main">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button"
            android:layout_marginBottom="65dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true" />
    </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

为什么会出现这种差异,我不知道,但是当您给布局提供宽度和高度时,它将在模拟器和工作室预览中显示相同。

 <include layout="@layout/content_main" 
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

答案 1 :(得分:1)

为预览和仿真器选择相同的分辨率,英寸和像素 通过预览窗口中的设备编辑器

相关问题