آموزش های این وب سایت به صورت رایگان در دسترس است. اطلاعات بیشتر
بروز خطا
   [message]
اشتراک در سوال
رای ها
[dataList]

ساخت bottomSheetDialog بدون استفاده از کتابخانه design

124847  8 سال پیش  8 سال پیش
+3 0

سلام میخوام طریقه ساخت bottomSheetDialog رو بدون استفاده از کتابخانه دیزاین بهتون بگم امیدوارم که دوست داشته باشین:

قبلا شیوه ی ساخت bottomSheetDialog رو با استفاده از کتابخونه دیزاین رو نوشته بودم که در لینک زیر موجود است:

https://answers.uncox.com/android/question/26433/%D8%B3%D8%A7%D8%AE%D8%AA-bottomsheetdialog-%D8%B4%D8%A8%DB%8C%D9%87-%D8%A7%DB%8C%D9%86%D8%B3%D8%AA%D8%A7%DA%AF%D8%B1%D8%A7%D9%85

اما اضافه کردن کتابخونه باعث افزایش حجم برنامه میشه و اگر دوست نداشته باشین حجم برنامه تون افزایش پیدا کنه از این روشی که میگم استفاده کنید:

ابتدا لایه ی bottomSheetDialog مورد نطر رو بسازید لایه من به این شکله:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_sheet"
android:orientation="vertical">


<TextView
android:id="@+id/txtShare"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="Share to Facebook"
android:textColor="@color/btnColor"
android:textSize="16dp"
android:background="@drawable/btn_top"/>




<TextView
android:id="@+id/txtTweet"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="Tweet"
android:textColor="@color/btnColor"
android:textSize="16dp"
android:background="@drawable/btn_middle"/>


<View
android:layout_width="match_parent"
android:layout_height="0.25dp"
android:background="@color/khatColor" />

<TextView
android:id="@+id/txtCopy"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="Copy Linlk"
android:textColor="@color/btnColor"
android:background="@drawable/btn_middle"/>


<TextView
android:id="@+id/txtReport"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="Report"
android:textColor="@color/Error"
android:textSize="16dp"
android:background="@drawable/btn_bottom1"/>


</LinearLayout>


<TextView
android:id="@+id/txtCancel"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:text="Cancel"
android:textColor="@color/textColor"
android:textSize="16dp"
android:background="@drawable/btn_bottom"
android:layout_marginTop="10dp"/>


</LinearLayout>


</RelativeLayout>

بک گراندهایی هم که برای عناصر موجود در لایه ام استفاده کردم:

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

<solid android:color="@color/CcolorBackground"/>
<corners android:radius="10dp"/>
<stroke android:color="@color/khatColor" android:width="0.25dp"/>

</shape>

>>>>>>background btn top<<<<<<<<<


<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="false">

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/CcolorBackground"/>
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<stroke android:color="@color/khatColor" android:width="0.25dp"/>


</shape>

</item>

<item android:state_pressed="true">

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/btnPressed"/>
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<stroke android:color="@color/khatColor" android:width="0.25dp"/>

</shape>

</item>

</selector>
>>>>>>>>btn middle<<<<<<<<
<?xml version="1.0" encoding="utf-8"?>


<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="false">

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/CcolorBackground" />

</shape>

</item>

<item android:state_pressed="true">

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/btnPressed" />


</shape>

</item>


</selector>
>>>>>>>>>btn bottom1<<<<<<<<
<?xml version="1.0" encoding="utf-8"?>


<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="false">

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/CcolorBackground"/>
<corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>
<stroke android:color="@color/khatColor" android:width="0.25dp"/>


</shape>

</item>

<item android:state_pressed="true">

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/btnPressed"/>
<corners android:bottomRightRadius="10dp" android:bottomLeftRadius="10dp"/>
<stroke android:color="@color/khatColor" android:width="0.25dp"/>

</shape>

</item>

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

<item android:state_pressed="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/CcolorBackground"/>
<corners android:radius="10dp"/>
<stroke android:color="@color/khatColor" android:width="0.25dp"/>

</shape>

</item>
<item android:state_pressed="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android">

<solid android:color="@color/btnPressed"/>
<corners android:radius="10dp"/>
<stroke android:color="@color/khatColor" android:width="0.25dp"/>

</shape>

</item>

</selector>

حالا 2 تا انیمیشن میخواهیم یک برای ظاهر شدن و دیگری برای رفتن دیالوگ ... انمیشن هایی که من طراحی کردم این شکلی هستن:

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

<translate android:fromXDelta="0" android:fromYDelta="100%"
android:toXDelta="0" android:toYDelta="0" android:duration="300"/>

<alpha android:fromAlpha="0" android:toAlpha="1" android:duration="150"/>

</set>

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


<translate android:fromXDelta="0" android:fromYDelta="0"
android:toXDelta="0" android:toYDelta="150%" android:duration="500"/>

<alpha android:fromAlpha="1" android:toAlpha="0" android:duration="300"/>


</set>

حالا باید انیمیشن های ساخته شده رو در قالب یک استایل تعریف کنیم به این شکل:

<style name="Bottom">
<item name="android:windowEnterAnimation">@anim/come_bottom</item>
<item name="android:windowExitAnimation">@anim/go_bottom</item>
</style>

فایل رنگی که من استفاده کردم:

<color name="CcolorBackground">#F9F9F9</color>
<color name="btnPressed">#EAEAEA</color>
<color name="btnColor">#0977FA</color>
<color name="textColor">#010101</color>
<color name="khatColor">#D5D5D5</color>
<color name="Error">#ba4040</color>

خب حالا میریم سراغ تعریف dialog:

من دیالوگ رو درون رخداد کلیک یک دکمه داخل یک تابع نوشتم شما میتونین این کارو نکنید و تابعی که دیالوگ رو تعریف کردم به این شکله:

private void dialogSheet(){

final Dialog dialog = new Dialog(MainActivity.this);
//از بین بردن title دیالوگ
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//از بین بردن بک گراند دیالوگ
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//معرفی لایه مورد نظر به دیالوگ ساخته شده
dialog.setContentView(R.layout.bottom_alert);
//دادن انیمیشن مورد نظر به دیالوگ
dialog.getWindow().getAttributes().windowAnimations = R.style.Bottom;

//از بین بردن فاصله پبش فرض از اطراف صفحه نمایشگر
dialog.getWindow().getAttributes().width = LinearLayout.LayoutParams.MATCH_PARENT;

//Grivity
dialog.getWindow().getAttributes().gravity = Gravity.BOTTOM;


final TextView txtShare = (TextView)dialog.findViewById(R.id.txtShare);
final TextView txtTweet = (TextView)dialog.findViewById(R.id.txtTweet);
final TextView txtCopy = (TextView)dialog.findViewById(R.id.txtCopy);
final TextView txtReport = (TextView)dialog.findViewById(R.id.txtReport);
final TextView txtCancel = (TextView)dialog.findViewById(R.id.txtCancel);


txtShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();

}
});

txtTweet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();

}
});


txtCopy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();

}
});

txtReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

dialog.dismiss();

}
});

txtCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

dialog.dismiss();

}
});


//نمایش دیالوگ
dialog.show();


}

خروجی برنامه:

امیدوارم لذت برده باشید

 برای این سوال پاسخی وجود ندارد.

پاسخگویی و مشاهده پاسخ های این سوال تنها برای اعضای ویژه سایت امکان پذیر است .
چنانچه تمایل دارید به همه بخش ها دسترسی داشته باشید میتوانید از این بخش لایسنس این آموزش را خریداری نمایید .