2017-10-12 72 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"/>