android -constraintLayout, как добавить высоту на липкой панели инструментов? - PullRequest
0 голосов
/ 05 февраля 2020

Я создал constraintLayout, но используемая мной панель инструментов не имеет высоты теней. Я не вижу тени. вот изображение того, что у меня есть:

enter image description here

я хочу, чтобы зеленая часть была приподнята и немного тени. Вы знаете способ заголовка материала. поэтому, когда я прокручиваю просмотрщик, он выглядит как заголовок. довольно стандартный в наши дни.

вот мой код:

 <?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">

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/gl_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_begin="76dp" />

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp"
    app:elevation="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintBottom_toTopOf="@id/gl_start"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@android:drawable/btn_star"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.appcompat.widget.Toolbar>


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/gl_start"
    tools:itemCount="10"
    tools:listitem="@layout/list_item" />

обратите внимание, что панель инструментов должна быть липкой, поэтому она не должна двигаться, но ей просто нужно подняться Я попытался установить высоту в xml на панели инструментов, но, как вы можете видеть, это не так красиво. как поставить тень и высоту?

Ответы [ 2 ]

1 голос
/ 05 февраля 2020

Либо заверните вас Toolbar внутри AppBarLayout

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar_layout"
    ...>

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            .../>
</com.google.android.material.appbar.AppBarLayout>

Или используйте android:elevation с Toolbar, что требует минимального уровня API 21

android:elevation="10dp"
0 голосов
/ 05 февраля 2020

попробуйте, как показано ниже -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="top"
              android:orientation="vertical">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
                                       android:layout_width="match_parent"
                                       android:layout_height="wrap_content"
                                       android:background="@color/shadow"
                                       android:titleTextAppearance="@color/White"
                                       app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent">

        <!-- your components -->

        <View android:layout_width="match_parent"
              android:layout_height="5dp"
              android:background="@drawable/toolbar_shadow"/>

    </FrameLayout>

</LinearLayout>

@ drawable / toolbar_shadow:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle">

    <gradient android:startColor="@android:color/transparent"
              android:endColor="#88333333"
              android:angle="90"/>

</shape>

@ color / shadow

<color name="shadow">#e74c3c</color>
...