2017-09-15 68 views
2

好的,這裏是我的活動的XML。有一個問題學習約束佈局

<?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" 
    tools:context="com.example.xxx.listview.MainActivity"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="0dp" 
     android:layout_height="120dp" 
     android:background="@android:color/holo_red_light" 
     android:minHeight="?attr/actionBarSize" 
     android:theme="?attr/actionBarTheme" 
     app:layout_constraintBottom_toTopOf="@+id/main_listview" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <ListView 
     android:id="@+id/main_listview" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     tools:layout_constraintBottom_creator="1" 
     tools:layout_constraintLeft_creator="1" 
     tools:layout_constraintRight_creator="1" 
     tools:layout_constraintTop_creator="1"></ListView> 


</android.support.constraint.ConstraintLayout> 

這是我結束的觀點。

What the look looks like when rendered in the emulator.

剛學,所以我是新來的約束東西。他們的工作方式與我的iOS生活有點不同,並試圖學習。

回答

0

當然,你有錯誤。我看到了你的佈局幾個問題:

  1. 你缺少你ListViewconstraintTop - >您ListView將成爲wrap_content高度和底部對齊。
  2. 你告訴你的ToolBarconstraintTopParentconstraintBottomListView - >這就是爲什麼你ToolBar是在頂部ListView和頂部Parent中間。

爲了解決您的問題,只是刪除您toolBarconstraintBottom並添加ListViewconstraintTop到工具欄的底部。

0

ListView缺少在其頂部約束:

app:layout_constraintTop_toBottomOf="@+id/toolbar" 

佈局工具應某處顯示一個警告信息,告訴你這將被重新定位或將是錯誤的大小,因爲你的身高是0dp。它會告訴你添加另一個約束。

如果你的身高是0,那麼你需要在視圖的頂部和底部有一個約束。如果您的寬度爲0,則需要對視圖的左側和右側進行約束。

0
  • 試試這個(在腦海使用約束熊我們已經4-constraintstop,bottom,left and Right
  • 我們必須使用像

<?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" 
android:id="@+id/cl_layout" 
tools:context="com.example.xxx.listview.MainActivity"> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/holo_red_light" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="?attr/actionBarTheme" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintBottom_toTopOf="@+id/main_listview"/> 

<ListView 
    android:id="@+id/main_listview" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    app:layout_constraintBottom_toBottomOf="parent" 
    app:layout_constraintLeft_toLeftOf="parent" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/toolbar"></ListView> 

+0

您忘記了ListView中的頂部約束 –