[英]How can I achieve this layout with a CoordinatorLayout, EditText, and ListView
I am switching over to a CoordinatorLayout
from a RelativeLayout
so I can take advantage of the FAB and moving with a Snackbar
.我正在从
RelativeLayout
切换到CoordinatorLayout
,这样我就可以利用 FAB 并使用Snackbar
移动。 My issue is that I was able to use layout_below
to give hierarchy and order to the items within my layout.我的问题是我能够使用
layout_below
为布局中的项目提供层次结构和顺序。 But, when I ran the app with a CoordinatorLayout, everything seems to go haywire and overlap.但是,当我使用 CoordinatorLayout 运行应用程序时,一切似乎 go 混乱并重叠。 I tried adding padding, but would rather use layout_below to guarantee that everything will function 100%.
我尝试添加填充,但宁愿使用 layout_below 来保证一切都将 function 100%。
And wrapping some elements in a RelativeLayout
doesn't seem to help at all.并且在
RelativeLayout
中包装一些元素似乎根本没有帮助。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_gap"
android:paddingBottom="20dp"
android:orientation="horizontal">
<EditText
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight="1"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:focusedByDefault="true"
android:hint="@string/input_a_number"
android:imeOptions="actionDone|flagNoPersonalizedLearning"
android:importantForAutofill="no"
android:inputType="numberSigned|numberDecimal"
android:maxLength="30"
android:maxLines="1"
android:singleLine="true"
tools:ignore="LabelFor"
tools:targetApi="o" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/default_gap"
android:layout_weight="1"
android:minWidth="180dp" />
</LinearLayout>
<LinearLayout
android:paddingTop="20dp"
android:layout_below="@id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearParent">
<ListView
android:id="@+id/listview"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp" />
</LinearLayout>
[![<!-- android:nestedScrollingEnabled="true"-->][1]][1]
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:fabSize="auto"
android:scaleType="fitXY"
app:borderWidth="0dp"
app:useCompatPadding="true"
android:visibility="gone"
app:backgroundTint="@color/jade_fab"
app:elevation="4dp"
app:srcCompat="@drawable/jade" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Replace your xml file with this layout file.用此布局文件替换您的 xml 文件。 It should do the job!
它应该做的工作!
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="@+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_gap"
android:paddingBottom="20dp"
android:orientation="horizontal">
<EditText
android:id="@+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_weight="1"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:focusedByDefault="true"
android:hint="@string/input_a_number"
android:imeOptions="actionDone|flagNoPersonalizedLearning"
android:importantForAutofill="no"
android:inputType="numberSigned|numberDecimal"
android:maxLength="30"
android:maxLines="1"
android:singleLine="true"
tools:ignore="LabelFor"
tools:targetApi="o" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/default_gap"
android:layout_weight="1"
android:minWidth="180dp" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="20dp"
android:layout_below="@id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linearParent">
<ListView
android:id="@+id/listview"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp" />
</LinearLayout>
</LinearLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
app:fabSize="auto"
android:scaleType="fitXY"
app:borderWidth="0dp"
app:useCompatPadding="true"
android:visibility="gone"
app:backgroundTint="@color/jade_fab"
app:elevation="4dp"
app:srcCompat="@drawable/jade" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.