Android Studio:预览和模拟器模式之间布局的差异

时间:2017-08-05 09:42:07

标签: android constraints

我刚刚进入Android应用程序开发世界并最近创建了我的第一个计算器应用程序。当我在模拟器上运行应用程序时,我看到了一个问题,即预览模式下的布局与模拟器模式中的布局之间存在差异,如下所示:

enter image description here

这是我的content_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main"
    tools:context="com.example.sonduong.calculator.MainActivity">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="of"
        android:textSize="24sp"
        tools:layout_editor_absoluteX="181dp"
        tools:layout_editor_absoluteY="255dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="%"
        android:textSize="24sp"
        tools:layout_editor_absoluteX="300dp"
        tools:layout_editor_absoluteY="207dp" />

    <EditText
        android:id="@+id/numberTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Number"
        android:inputType="numberDecimal"
        android:textAlignment="center"
        tools:layout_editor_absoluteX="85dp"
        tools:layout_editor_absoluteY="296dp" />

    <TextView
        android:id="@+id/totalTextView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="0"
        android:textAlignment="center"
        android:textSize="36sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.101" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="What is"
        android:textSize="24sp"
        tools:layout_editor_absoluteX="152dp"
        tools:layout_editor_absoluteY="129dp" />

    <EditText
        android:id="@+id/percentageTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter Percentage"
        android:inputType="numberDecimal"
        android:textAlignment="center"
        tools:layout_editor_absoluteX="85dp"
        tools:layout_editor_absoluteY="193dp" />

    <Button
        android:id="@+id/calcBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CALCULATE"
        tools:layout_editor_absoluteX="147dp"
        tools:layout_editor_absoluteY="374dp" />

</android.support.constraint.ConstraintLayout>

但是在这个xml文件中我也遇到了一些错误,它们看起来像是:

enter image description here

任何人都可以帮助我如何调整以在预览和模拟器之间拥有相同的视图。谢谢!

2 个答案:

答案 0 :(得分:2)

根据您的需要进行调整。

XML替换为此XML

<?xml version="1.0" encoding="utf-8"?>
<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">

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="of"
    android:textSize="24sp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    android:layout_marginBottom="92dp"
    app:layout_constraintBottom_toTopOf="@+id/numberTxt" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="%"
    android:textSize="24sp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBaseline_toBaselineOf="@+id/percentageTxt"
    android:id="@+id/textView2" />

<EditText
    android:id="@+id/numberTxt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Enter Number"
    android:inputType="numberDecimal"
    android:textAlignment="center"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintHorizontal_bias="0.505"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toTopOf="@+id/calcBtn" />

<TextView
    android:id="@+id/totalTextView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="0"
    android:textAlignment="center"
    android:textSize="36sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    android:layout_marginTop="8dp" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="What is"
    android:textSize="24sp"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/totalTextView"
    app:layout_constraintHorizontal_bias="0.502" />

<EditText
    android:id="@+id/percentageTxt"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Enter Percentage"
    android:inputType="numberDecimal"
    android:textAlignment="center"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/textView2"
    android:layout_marginRight="8dp"
    app:layout_constraintHorizontal_bias="0.0"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/textView3"
    android:layout_marginTop="8dp"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

<Button
    android:id="@+id/calcBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="CALCULATE"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="9dp"
    app:layout_constraintHorizontal_bias="0.502" />

答案 1 :(得分:1)

好的,你需要使用class UserAuth(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE')) # User authentication information username = db.Column(db.String(50), nullable=False, unique=True) password = db.Column(db.String(255), nullable=False, default='') # Relationships user = db.relationship('User', uselist=False) def hash_password(self, password): self.password_hash = pwd_context.encrypt(password) def verify_password(self, password): return pwd_context.verify(password, self.password_hash) def __str__(self): return unicode(self).encode('utf-8') def __unicode__(self): return self.username 来加权,试试这个代码..

linear layout