下面的滾動視圖忽略了app:layout_constraintTop_toBottomOf="@id/topView"約束。我希望滾動視圖位于下方topView,我該怎么做?
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"">
<TextView
android:id="@ id/topView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello!" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="@id/topView"> <!-- has no effect! -->
<LinearLayout
android:id="@ id/vertical_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@id/topView"> <!-- has no effect! -->
<!-- TextViews in LinearLayout here -->
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
這是一個簡單的可滾動線性布局。
uj5u.com熱心網友回復:
正如評論中所說,您不能將約束應用于不是ConstraintLayout.
它沒有做任何事情的第二部分是因為你設定layout_height了ScrollViewtomatch_parent所以它只是強迫自己超過TextView.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"">
<TextView
android:id="@ id/topView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello!"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/topView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<LinearLayout
android:id="@ id/vertical_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
<!-- TextViews in LinearLayout here -->
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/522125.html
上一篇:從自定義視圖更改布局屬性時出現NullPointerException
下一篇:如何將RelativeLayout(centerInParent)和圖層串列(gravity:center)的居中匹配為windowBackground?
