[英]How to align a Linear Layout at the bottom with a “match parent” scroll view on top?
我有一個問題,我想在屏幕底部放置一個圖像。 如何正確放置@+id/bottomImage
?
這是代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.akrodyn.Innoproject_2021.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="82dp"
android:src="@drawable/pdz" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="#000000"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="20dp"
android:text="Search"
app:backgroundTint="#126FB7" />
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:text="Connect"
app:backgroundTint="#126FB7" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="15dp"
android:layout_marginEnd="10dp"
android:layout_weight="0"
android:layout_above="@+id/bottomImage"
android:background="#FFFFFF"></ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_marginEnd="0dp"
android:layout_weight="1"
android:gravity="center|bottom"
android:id="@+id/bottomImage"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@drawable/Instructions" />
</LinearLayout>
</LinearLayout>
我一直在玩,但編輯器沒有向我展示正確的設置。 似乎滾動視圖以某種方式位於 imageView 之上? 如果我將滾動視圖的高度設置為某個值,我會得到我想要的,但這當然取決於手機。 我想讓它成為相對的,這樣無論屏幕大小如何它都能正常工作。
如果您使用ConstraintLayout
會更容易。 所以我猜你正在尋找這樣的東西:
<?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="com.akrodyn.Innoproject_2021.MainActivity">
<LinearLayout
android:id="@+id/topFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="82dp"
android:src="@drawable/pdz" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="7dp"
android:background="#000000"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="@+id/listview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/topFrame">
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="20dp"
android:text="Search"
android:backgroundTint="#126FB7"/>
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:text="Connect"
android:backgroundTint="#126FB7" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="@+id/bottomImage"
android:layout_weight="0"
android:background="#FFFFFF"
app:layout_constraintBottom_toTopOf="@+id/bottomImage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2">
</ListView>
<LinearLayout
android:id="@+id/bottomImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@drawable/Instructions" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
另外,我認為您有充分的理由將單個 ImageView 放在 LinearLayout 中,所以我沒有更改它。 但是,如果您不這樣做,則無需這樣做,您可以將其中一些 ImageView 從 LinearLayouts 中取出。
要求:列表視圖必須在底部圖像上方和按鈕的線性布局下方對齊。
用Constraint Layout寫代碼絕對可以讓它變得更好。 如果您想以 root 身份查看相對布局代碼中的問題,那么這會有所幫助。 對您的代碼進行一些修改可以幫助我們實現這一目標。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.akrodyn.Innoproject_2021.MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="82dp"
android:src="@drawable/pdz" />
<LinearLayout
android:id="@+id/llButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_marginTop="15dp"
android:background="#000000"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginEnd="20dp"
android:text="Search"
app:backgroundTint="#126FB7" />
<Button
android:id="@+id/connect"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginStart="20dp"
android:text="Connect"
app:backgroundTint="#126FB7" />
</LinearLayout>
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottomImage"
android:layout_below="@id/llButtons"
android:layout_marginTop="15dp"
android:layout_marginEnd="10dp"
android:background="#FFFFFF" />
<LinearLayout
android:id="@+id/bottomImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="0dp"
android:gravity="center|bottom"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="@drawable/Instructions" />
</LinearLayout>
</RelativeLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.