[英]What is the most efficient way to accomplish two panel layout with elevation?
因此,我使用ConstraintLayout
尝试了不同的变体以使其尽可能保持平坦,但下面的代码是我能完成的最好的。 此外,我需要添加一个ScrollView
作为布局父项。 如果有人可以帮助我,我将不胜感激。 提前致谢!
这是我目前拥有的 xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="hardCodedText, contentDescription">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:paddingBottom="20dp"
android:elevation="@dimen/elevation">
<TextView
android:id="@+id/tv_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
style="@style/NDL.Typography.Standard02"
android:text="Hello there, help us fill out this information"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/name_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@id/tv_header"
app:layout_constraintStart_toStartOf="@id/tv_header"
android:hint="Name">
<EditText
android:id="@+id/name_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:paddingBottom="16dp"
style="NDL.Typography.Large01"/>
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/name_info_icon"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginEnd="20dp"
android:src="@drawable/ic_icon_info"
android:tint="@color/blue_02"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/name_layout"
app:layout_constraintBottom_toBottomOf="@+id/name_layout"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/address_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/name_layout"
app:layout_constraintStart_toStartOf="@+id/name_layout"
android:hint="Address">
<EditText
android:id="@+id/address_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:paddingBottom="16dp"
style="NDL.Typography.Large01"/>
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/address_icon_info"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginEnd="20dp"
android:src="@drawable/ic_icon_info"
android:tint="@color/blue_02"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/address_layout"
app:layout_constraintBottom_toBottomOf="@+id/address_layout"/>
<android.support.design.widget.TextInputLayout
android:id="@+id/phone_number_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp"
android:layout_marginTop="16dp"
android:elevation="@dimen/elevation"
app:layout_constraintTop_toBottomOf="@+id/address_layout"
app:layout_constraintStart_toStartOf="@+id/address_layout"
android:hint="Phone number">
<EditText
android:id="@+id/phone_number_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:paddingBottom="20dp"
style="NDL.Typography.Large01"/>
</android.support.design.widget.TextInputLayout>
<ImageView
android:id="@+id/phone_number_icon"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginEnd="20dp"
android:tint="@color/blue_02"
android:src="@drawable/ic_icon_info"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/phone_number_layout"
app:layout_constraintBottom_toBottomOf="@+id/phone_number_layout"/>
</android.support.constraint.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/gray_02">
<TextView
android:id="@+id/footer"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:layout_margin="16dp"
style="NDL.Typography.Standard01"
android:text="Make sure to keep your information up to date."/>
<Button
style="@style/NDL.Button.Primary"
android:id="@+id/bt_confirm_button"
android:layout_weight="0"
android:layout_height="36dp"
android:layout_width="match_parent"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginBottom="20dp"
android:text="CONFIRM"/>
</LinearLayout>
</LinearLayout>
这就是我想要完成的:
您可以使用此代码:
<ScrollView 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:orientation="vertical">
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="222dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
您可以将现有代码放入 ScrollView,但问题是您将无法设置 LinearLayout height="match_parent",因此我建议将其设置为任何默认值,然后在您的活动中按语法设置它的高度作为当前用户手机屏幕的高度乘以 3,使用如下代码:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
DisplayMetrics metrics = new DisplayMetrics();
linearLayout.getLayoutParams().height = metrics.heightPixels * 3;
是的,它是一种硬代码 ))) 我相信你的布局不会超过 3 个屏幕尺寸......试试吧,它必须工作 )))
您可以尝试使用带有指南和 cardView 的 constLayout ,如下所示:
<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"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
android:id="@+id/frameLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color"
tools:context=".Fragments.MenusDesign.ExpandableCategoriesMenu.ExpandableCategoriesMenu"
xsi:schemaLocation="http://schemas.android.com/apk/res/android ">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@color/colorAccent"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@+id/tv_header"
android:layout_width="342dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:elevation="2dp"
android:text="Hello there, help us fill out this information"
app:layout_constraintEnd_toEndOf="@+id/cardView"
app:layout_constraintStart_toStartOf="@+id/button4"
app:layout_constraintTop_toTopOf="@+id/cardView" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/bt_confirm_button"
android:layout_width="272dp"
android:layout_height="48dp"
android:layout_marginStart="28dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:layout_weight="0"
android:elevation="2dp"
android:text="CONFIRM"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Make sure to keep all your info private"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:elevation="2dp"
android:text="phone number"
app:layout_constraintBottom_toBottomOf="@+id/cardView"
app:layout_constraintEnd_toEndOf="@+id/button4"
app:layout_constraintStart_toStartOf="@+id/button4" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:elevation="2dp"
android:text="Button"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="@+id/cardView"
app:layout_constraintStart_toStartOf="@+id/cardView"
app:layout_constraintTop_toBottomOf="@+id/button5" />
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="Name"
app:layout_constraintEnd_toEndOf="@+id/button4"
app:layout_constraintStart_toStartOf="@+id/button4"
app:layout_constraintTop_toBottomOf="@+id/tv_header" />
现在您需要做的就是为您的 cardView 提供您的设计(阴影、颜色等...),并且您可以在不使用任何嵌套布局的情况下很好地提升屏幕的上半部分。
您的布局将如下所示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.