【发布时间】:2019-10-08 16:46:30
【问题描述】:
我有一个扩展 BottomSheetDialogFragment 的自定义类,该类将在单击按钮时显示。 我的自定义 BottomSheetDialogFragment 布局有 3 个部分。
a.A 标题文本,
b.一个单选组(我动态添加 n 个项目)
c.底部的OK按钮(我想一直显示在底部)
实际上在我的对话框片段第一次启动时,我的 OK 按钮是不可见的。但是当我展开 BottomSheet 时,它看起来像下面,并且我的 OK 按钮是可见的
但我需要的是始终显示我的底部 OK 按钮,即当我的对话框片段启动时,我的 OK 按钮应该出现在底部,而不管它有多少单选按钮以及何时展开,那么确定按钮也应该在底部,我的单选按钮应该是可滚动的。
下面是我的对话框片段布局:
<?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="wrap_content"
>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/nested_scroll_view"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:layout_marginRight="20dp"
android:layout_alignParentTop="true"
android:text=""
/>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_below="@id/title"
android:layout_marginBottom="10dp"
>
</RadioGroup>
<android.widget.Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ok"
android:layout_below="@id/radiogroup"
android:text="OK"
android:layout_marginTop="10dp"
android:layout_alignParentBottom="true"
></android.widget.Button>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
这是我的自定义 BottomSheetDialogFragment
public class BottomSheetExample extends BottomSheetDialogFragment {
@BindView(R.id.title)
TextView title;
@BindView(R.id.ok)
Button ok;
@BindView(R.id.nested_scroll_view)
NestedScrollView nestedScrollView;
@BindView(R.id.radiogroup)
RadioGroup radioGroup;
// TODO: Rename and change types of parameters
public BottomSheetExample() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_sheet, container, false);
ButterKnife.bind(this, view);
ArrayList<String> list = new ArrayList<>();
for(int i=0;i<15;i++){
list.add(""+i);
}
title.setText("Numbers");
RadioGroup rg = radioGroup;
for (int i = 0; i < list.size(); i++) {
RadioButton rbn = new RadioButton(getContext());
rbn.setId(View.generateViewId());
String radioButtonText = list.get(i);
rbn.setText(radioButtonText);
rg.addView(rbn);
}
return view;
}
}
这就是我如何称呼我的底页:
BottomSheetExample bottomSheet = new BottomSheetExample();
bottomSheet.showNow(this.getSupportFragmentManager(), "tag");
任何输入都将非常有用。提前致谢!
【问题讨论】:
-
我也遇到了同样的问题,你找到解决办法了吗?
-
@Himanshu 我已经添加了我的解决方案。
-
@Himanshu,如果对你有帮助,请点赞我的回答
标签: android android-fragments android-scrollview bottom-sheet android-nestedscrollview