2017-10-12 74 views
0

在我的预览中一切看起来不错,但是当我在模拟器中打开应用程序时,布局与长度或宽度的父级不匹配。一切都在它应该在布局中的位置,它不能与屏幕整体拉伸。这发生在应用程序的每个布局中(我正在使用片段)。为了更好地解释继承人PIX:Android Studio布局。将不匹配父母

**什么是预览(并且是我想要什么) enter image description here

**我有什么

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:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 


    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="center" 
     android:src="@drawable/hlineand"/> 

    <TextView 
     android:id="@+id/userTextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="User" 
     android:textAlignment="center" 
     android:textAppearance="@android:style/TextAppearance.Material.Small" 
     android:textColor="@android:color/darker_gray" 
     android:textSize="30dp"/> 

    <TextView 
     android:id="@+id/updateTextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentEnd="true" 
     android:layout_alignParentStart="true" 
     android:text="----" 
     android:textAlignment="center"/> 

    <EditText 
     android:id="@+id/emailEditText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="22dp" 
     android:ems="10" 
     android:hint="e-mail" 
     android:inputType="textEmailAddress" 
     android:layout_below="@+id/userTextView" 
     android:layout_centerHorizontal="true"/> 

    <EditText 
     android:id="@+id/passwordEditText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignStart="@+id/emailEditText" 
     android:layout_below="@+id/emailEditText" 
     android:layout_marginTop="24dp" 
     android:ems="10" 
     android:hint="password" 
     android:inputType="textPassword"/> 

    <Button 
     android:id="@+id/loginButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/passwordEditText" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:text="Login"/> 
</RelativeLayout> 

这是我的内容main.xml。就像第一位提到的评论者那样,它就在那里。它工作得很好,当我把匹配父&匹配的父母,但不是它的调整大小像宽度344px &高度590px。任何方式,我可以保持作为配对父母?

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

    <FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="344dp" 
     android:layout_height="495dp" 
     tools:layout_editor_absoluteY="8dp" 
     tools:layout_editor_absoluteX="8dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/> 

</android.support.constraint.ConstraintLayout> 
+0

你需要发表您的主要布局,我的意思是布局包含片段容器,请参阅容器ISN” t设置为match_parent –

+0

@ Du.Fantasy我将我的主要内容添加进去了,你已经准备好了要去的地方。 –

+0

你是什么意思,但不是它调整大小像宽度344px和高度590px“,我不清楚你的目的 –

回答

1

变化FrameLayout从固定宽度和高度

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

     <FrameLayout 
      android:id="@+id/content_frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:layout_editor_absoluteY="8dp" 
      tools:layout_editor_absoluteX="8dp"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:layout_constraintBottom_toBottomOf="parent" 
      app:layout_constraintLeft_toLeftOf="parent" 
      app:layout_constraintRight_toRightOf="parent" 
      app:layout_constraintTop_toTopOf="parent"/> 

    </android.support.constraint.ConstraintLayout> 
0

按我的知识match_parent,在你的第一个XML,你是给orientation="vertcial"相对布局。如果你希望它可以工作,那么你应该使用线性布局,并使用该布局的方向。

尝试在你内容XML使用这样的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_blood_g_activity" 
android:layout_width="match_parent" 
android:orientation="vertical" 
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"> 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Mature Diabetic Patient" 
    android:layout_gravity="center" 
    android:textSize="15dp"/> 


<View 
    android:layout_width="wrap_content" 
    android:layout_height="30dp"/> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="horizontal"> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Enter Your current Blood Sugar"/> 
    <View 
     android:layout_width="20dp" 
     android:layout_height="60dp"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/cbs" 
     android:inputType="number" 
     android:hint="mg/dl"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="horizontal"> 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Enter Your Target Blood Sugar"/> 
    <View 
     android:layout_width="20dp" 
     android:layout_height="60dp"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tbs" 
     android:inputType="number" 
     android:hint="mg/dl"/> 

</LinearLayout> 
<Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Humalog/Novalog" 
     android:id="@+id/btnhnbs" 
     android:layout_gravity="center"/> 
    </LinearLayout> 
2

修正内容主XML通过改变宽度高度0dp代替匹配父&在框架布局的底部加入4个约束。谢谢你们,谢谢你们,以及到@ Du.fantasy指着我在正确的方向这么快

<FrameLayout 
     android:id="@+id/content_frame" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     tools:layout_editor_absoluteY="8dp" 
     tools:layout_editor_absoluteX="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/>