سلام خدمت همه عزیزان کدایت ، امروز قصد داریم از سری آموزش کاتلین مبحث Bottom Sheet در اندروید را برای شما عزیزان آموزش دهیم .
Bottom Sheet در اندروید چیه؟ و در چه مواردی مورد استفاده قرار میگیرد ؟
برای همه برنامه نویسان موبایل فارغ از استفاده از هر زبانی نیاز هست ، در مواردی از یکسری دیالوگ ها و منو های برای نمایش دادن آیتم های مورد نیاز استفاده کنند و اما Bottom Sheet در اندروید به جای این Dialog های قدیمی آمده و شما برنامه نویسان موبایل به راحتی میتوانید آن را پیاده سازی کنید و در برنامه اندرویدی خودتون استفاده کنید . باتوم شیت به صورت یک پنجره از قسمت پاینی موبایل ظاهر میشود . و اون آیتم ها و گزینه های که شما در آن قرار دادید را برای کاربران نشان میدهد .

آموزش پیاده سازی Bottom Sheet در برنامه نویسی اندروید
در واقع Bottom Sheet ها با محتوای اصلی صفحه ی ما در ارتباط هستش و به نوعی یکسری امکانات بیشتری در اختیار کاربران قرار میدهد.بزارید یکم توضیحات بیشتری در مورد باتوم شیت خدمت شما عزیزان ارائه دهیم . فرض کنید ما در یک صفحه یک متن و یا پستی برای مخاطبان خودمون نمایش میدهیم . ولی در همان صفحه بغل محتوا یا همون پستمون چیزی قرار نمیدهیم .
ولی در مقابل کاربر میتوان با لمس یک دکمه در بالای پست و یا کلیک خود پست یک پنچره مشاهده کند تا امکانات زیادی را در مشاهده کند . مثلا ما میتوانیم هنگانی کاربران ما پستمان را با انگشت خود نکه میدارد یه Bottom Sheet ایجاد کنیم تا یک پنجره ایجاد شود و مخاطبان ما بتوانند آن محتوا را Copy ، Share و .. یا کار های دیگری انجام دهد . من امروز قصد دارم نحوی پیاده سازی Bottom Sheet در اندروید را برای شما عزیزان با انجام یک پروژه کوچک نشان دهم.
پیاده سازی Bottom Sheet :
اولین کاری که باید انجام دهیم اضافه کردن کتابخانه متریال دیزاین به پروژه خودمون هست تا بتوانیم از باتوم شیت استفاده کنیم . برای انجام این کار به قسمت build.gradle(Module) بروید و کتابخانه زیر را اضافه کنید .
implementation 'com.google.android.material:material:1.1.0'
بعد از این کار در لایه Xml یک Button میگذاریم تا با کلیک بر روی آن Bottom Sheet رویت شود . کد این قسمت مانند زیر هست .
<?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=".MainActivity" app:layout_behavior="com.google.android.material.BottomSheet" > <Button android:id="@+id/btn_showButtonSheet" android:layout_width="200dp" android:layout_height="50dp" android:text="ButtonSheet" android:layout_centerInParent="true" /> </RelativeLayout>
در مرحله بعد ما باید یک لایه برای طراحی باتوم شیت خودمون ایجاد کنیم . مطابق سلیقه و نیاز خودمون لایه ی باتوم شیت خودمون رو درست کنیم . مانند زیر.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="225dp" android:orientation="vertical" app:layout_behavior="com.google.android.material.BottomSheet"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="?selectableItemBackground" android:clickable="true" android:focusable="true" android:orientation="horizontal" android:padding="15dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_baseline_share_24" app:tint="@color/craycolor" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:text="share" android:textColor="@color/craycolor" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="?selectableItemBackground" android:clickable="true" android:focusable="true" android:orientation="horizontal" android:padding="15dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_baseline_insert_link_24" app:tint="@color/craycolor" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:text="link" android:textColor="@color/craycolor" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="?selectableItemBackground" android:clickable="true" android:focusable="true" android:orientation="horizontal" android:padding="15dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_baseline_content_copy_24" app:tint="@color/craycolor" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:text="copy" android:textColor="@color/craycolor" android:textSize="16dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="?selectableItemBackground" android:clickable="true" android:focusable="true" android:orientation="horizontal" android:padding="15dp"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_baseline_delete_24" app:tint="@color/craycolor" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="14dp" android:text="delete" android:textColor="@color/craycolor" android:textSize="16dp" /> </LinearLayout> </LinearLayout>
من در قسمت بالا برای باتوم شیت خودم از چند image استفاده کردم که شما هم میتوانید یا از بیرون دانلود کنید و یا از قسمت Vector Asset اضافه کنید .
و اما کد قسمت main Activity من که به صورت زیر میباشد .
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.LayoutInflater import android.widget.Button import androidx.core.view.LayoutInflaterCompat import com.google.android.material.bottomsheet.BottomSheetDialog class BottomSheet : AppCompatActivity() { lateinit var btn_ShowBottomSheet: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_bottom_sheet) btn_ShowBottomSheet = findViewById(R.id.btn_showButtonSheet) val bottomsheet = BottomSheetDialog(this) val view = layoutInflater.inflate(R.layout.layout_bottomsheet, null) bottomsheet.setContentView(view) btn_ShowBottomSheet.setOnClickListener { bottomsheet.show() } } }
و اما خروجی این پروژه به صورت زیر میباشد .امیدوارم برای شما عزیزان مفید بوده باشد .
موفق باشد .
دیدگاهتان را بنویسید