代码之家  ›  专栏  ›  技术社区  ›  Usman Ali

在Android Studio预览版中完美展示设计,但在真实设备中却没有

  •  1
  • Usman Ali  · 技术社区  · 6 年前

    我正在开发一个Android应用程序,我正在尝试在androidstudio中实现设计布局 Image in Android Studio Preview

    但当我在真正的设备上运行它时,它的布局就像 Design In Real Device

    在Android工作室中,我使用nexus4来设计布局,我的手机分辨率是720x1280。 这是我的xml代码

    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.usmanali.childsafety.MainActivity">
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="275sp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="3dp">
    
        <ImageView
            android:id="@+id/imgschool"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/im"
            android:contentDescription="@string/todo" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="115sp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="278dp">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/download" />
    </LinearLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="110sp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="393dp"
        tools:ignore="MissingConstraints">
    
        <Button
            android:id="@+id/btnlogin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/btnregister"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="@string/login"
            android:textStyle="bold"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="291dp" />
    
        <Button
            android:id="@+id/btnregister"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="@string/sign_up"
            android:textStyle="bold"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="340dp" />
    </RelativeLayout>
    

    1 回复  |  直到 6 年前
        1
  •  1
  •   Drashti Joshi    6 年前

    试试这个

    <android.support.constraint.ConstraintLayout 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"
        tools:context=".MainActivity">
    
    
        <LinearLayout
            android:id="@+id/mainLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="275sp"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="3dp">
    
            <ImageView
                android:id="@+id/imgschool"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/im"
                android:contentDescription="@string/todo" />
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="115sp"
           app:layout_constraintTop_toBottomOf="@+id/mainLinearLayout"
            tools:ignore="MissingConstraints"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="278dp">
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/download" />
        </LinearLayout>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="110sp"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="393dp"
            tools:ignore="MissingConstraints">
    
            <Button
                android:id="@+id/btnlogin"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/btnregister"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:text="@string/login"
                android:textStyle="bold"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="291dp" />
    
            <Button
                android:id="@+id/btnregister"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:text="@string/sign_up"
                android:textStyle="bold"
                tools:ignore="MissingConstraints"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="340dp" />
        </RelativeLayout>
    </android.support.constraint.ConstraintLayout>