【问题标题】:What is the most efficient way to accomplish two panel layout with elevation?用高程完成两个面板布局的最有效方法是什么?
【发布时间】:2019-08-07 00:15:22
【问题描述】:

所以,我尝试了使用 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>

这就是我想要完成的:

【问题讨论】:

  • 所以你想让你的布局的上部看起来像他有一些高度?
  • 是的,上面的代码完成了同样的工作,但效率不高,因为包含嵌套的视图组
  • 检查我的答案 - 我在里面有 0 个嵌套布局。

标签: android android-layout scrollview android-linearlayout android-elevation


【解决方案1】:

你可以使用这个代码:

<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 种屏幕尺寸...试试吧,它必须有效)))

【讨论】:

  • 解决方案是保持 LinearLayout 的高度 = wrap_content 并设置 android:fillViewport="true"。 :-)
  • 你的布局不是那么聪明——你在 ScrollView 的 LinearLayout 中有 ConstraintLayout,看看我上面的回答——我只使用了 1 个布局,而且它也是响应式的。这就是 ConstraintLayout 的魔力 - 无需嵌套布局即可使用的强大工具(它更适合执行)
【解决方案2】:

您可以尝试像这样使用带有指南和 cardView 的constraintLayout:

<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 提供您的设计(阴影、颜色等...),并且您的屏幕上半部分很好地提升了,而无需在内部使用任何嵌套布局。

您的布局将如下所示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多