自定义底部 Sheet 对话框的视图
Customization Bottom Sheet Dialog's View
我只想获得如下所示的 BottomSheetDialog
:来自系统的保证金 window。我怎么会变成这样?
您可以通过以下方式创建底部 Sheet 对话框片段:
首先创建xml文件,命名为
fragment_bottomsheet
<?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:background="@android:color/transparent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="20dp"
android:background="@drawable/round_corner_white3"
android:orientation="vertical">
<TextView
android:id="@+id/tv_select_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:background="@drawable/round_corner_gray"
android:layout_margin="10dp"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Select Address" />
</RelativeLayout>
</RelativeLayout>
现在创建一个底部 Sheet 片段,命名为
BottomSheetFragment
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.View;
public class BottomSheetFragment extends BottomSheetDialogFragment {
public static BottomSheetFragment newInstance() {
BottomSheetFragment fragment = new BottomSheetFragment();
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setupDialog(Dialog dialog, int style) {
View contentView = View.inflate(getContext(), R.layout.fragment_bottomsheet, null);
dialog.setContentView(contentView);
((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
}
要调用底部 sheet 片段,您可以按如下方式编写:
BottomSheetFragment bottomSheetDialog = BottomSheetFragment.newInstance();
bottomSheetDialog.show(getSupportFragmentManager(), "Bottom Sheet Dialog Fragment");
我现在只拍了一个 textview 并附上了屏幕截图,因为您主要关心的是在底部获得边距sheet。同样通过这种方式,您可以根据需要自定义 bottom sheet 。谢谢!
添加依赖
implementation 'com.baoyz.actionsheet:library:1.1.7'
在Activity
中添加这段代码
public void ButtonActionSheet() {
ActionSheet.createBuilder(this, getSupportFragmentManager())
.setCancelButtonTitle("Cancel")
.setOtherButtonTitles("item1", "item2")
.setCancelableOnTouchOutside(true)
.setListener(new ActionSheet.ActionSheetListener() {
@Override
public void onDismiss(ActionSheet actionSheet, boolean isCancel) {
}
@Override
public void onOtherButtonClick(ActionSheet actionSheet, int index) {
if (index == 0) {
myMethod();
} else if (index == 1) {
myMethod2();
}
}
})
.show();
}
在style.xml
中添加这一行
<item name="actionSheetStyle">@style/ActionSheetStyleiOS7</item>
我只想获得如下所示的 BottomSheetDialog
:来自系统的保证金 window。我怎么会变成这样?
您可以通过以下方式创建底部 Sheet 对话框片段:
首先创建xml文件,命名为
fragment_bottomsheet
<?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:background="@android:color/transparent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_margin="20dp"
android:background="@drawable/round_corner_white3"
android:orientation="vertical">
<TextView
android:id="@+id/tv_select_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="@color/white"
android:background="@drawable/round_corner_gray"
android:layout_margin="10dp"
android:layout_alignParentBottom="true"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="Select Address" />
</RelativeLayout>
</RelativeLayout>
现在创建一个底部 Sheet 片段,命名为
BottomSheetFragment
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.View;
public class BottomSheetFragment extends BottomSheetDialogFragment {
public static BottomSheetFragment newInstance() {
BottomSheetFragment fragment = new BottomSheetFragment();
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setupDialog(Dialog dialog, int style) {
View contentView = View.inflate(getContext(), R.layout.fragment_bottomsheet, null);
dialog.setContentView(contentView);
((View) contentView.getParent()).setBackgroundColor(getResources().getColor(android.R.color.transparent));
}
}
要调用底部 sheet 片段,您可以按如下方式编写:
BottomSheetFragment bottomSheetDialog = BottomSheetFragment.newInstance();
bottomSheetDialog.show(getSupportFragmentManager(), "Bottom Sheet Dialog Fragment");
我现在只拍了一个 textview 并附上了屏幕截图,因为您主要关心的是在底部获得边距sheet。同样通过这种方式,您可以根据需要自定义 bottom sheet 。谢谢!
添加依赖
implementation 'com.baoyz.actionsheet:library:1.1.7'
在Activity
中添加这段代码public void ButtonActionSheet() {
ActionSheet.createBuilder(this, getSupportFragmentManager())
.setCancelButtonTitle("Cancel")
.setOtherButtonTitles("item1", "item2")
.setCancelableOnTouchOutside(true)
.setListener(new ActionSheet.ActionSheetListener() {
@Override
public void onDismiss(ActionSheet actionSheet, boolean isCancel) {
}
@Override
public void onOtherButtonClick(ActionSheet actionSheet, int index) {
if (index == 0) {
myMethod();
} else if (index == 1) {
myMethod2();
}
}
})
.show();
}
在style.xml
中添加这一行 <item name="actionSheetStyle">@style/ActionSheetStyleiOS7</item>