Android 如何在底部工作表对话框片段中始终将按钮与屏幕底部对齐
我有一个自定义类,它将在单击按钮时显示。 我的自定义BottomSheetDialogFragment布局由3部分组成 a、 标题文本 b、 一个无线组(我动态地向其中添加了n个项目) c、 底部有一个OK按钮(我希望始终显示在底部) 这是我点击按钮时的外观。 实际上,当我的对话框片段第一次启动时,我的“确定”按钮不可见。但是,当我展开底部时,它看起来像下图,我的“确定”按钮可见 但我需要的是始终显示底部的“确定”按钮,即当启动对话框片段时,无论单选按钮的数量如何,并且在展开时,“确定”按钮都应显示在底部,并且我的单选按钮应可滚动 下面是我的对话框片段布局:Android 如何在底部工作表对话框片段中始终将按钮与屏幕底部对齐,android,android-fragments,android-scrollview,bottom-sheet,android-nestedscrollview,Android,Android Fragments,Android Scrollview,Bottom Sheet,Android Nestedscrollview,我有一个自定义类,它将在单击按钮时显示。 我的自定义BottomSheetDialogFragment布局由3部分组成 a、 标题文本 b、 一个无线组(我动态地向其中添加了n个项目) c、 底部有一个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>
任何输入都将非常有用。提前感谢 您可以尝试一下,将相对布局中包装的按钮放在嵌套滚动视图之外就可以了
<?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"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:layout_marginRight="20dp"
android:text="" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.widget.Button
android:id="@+id/ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginTop="10dp"
android:gravity="bottom"
android:text="OK" />
</RelativeLayout>
</LinearLayout>
最后,我找到了一个解决方案 1.为了正确使用底片,我使用了协调布局,而顶部线性布局具有底片行为 2.我已使radiogroup适合nestedscrollview。我希望nestedscrollview的最大高度是固定的。但遗憾的是,没有最大高度方法。因此,线性布局容纳嵌套的滚动视图,其中包含放射组。 现在我已经将线性布局的高度设置为300dp。布局部分已经完成 3.现在我们必须固定底板部分。从1中提到的顶部线性布局中获取底板行为实例。因此,现在将底板的窥视高度设置为300dp。这样做是为了将底板固定到特定高度,以适合问题中提到的用例 XML:
爪哇:
公共类BottomSheetExample扩展了BottomSheetDialogFragment{
@BindView(R.id.title)
文本视图标题;
@BindView(R.id.click\u btn)
按钮ok;
@BindView(R.id.nested\u scroll\u视图)
NestedScrollView NestedScrollView;
@BindView(R.id.radiogroup)
放射组放射组;
@BindView(R.id.底部\图纸\布局)
线性布局底板布局;
私人行为;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图=充气机。充气(右布局。底部,容器,假);
ButterKnife.bind(这个,视图);
sheetBehavior=BottomSheetBehavior.from(bottomSheetLayout);
sheetBehavior.setPeekHeight(R.dimen.peek_height);//放置此ub dimens.xml(300dp)
ArrayList=新建ArrayList();
对于(int i=0;i<15;i++){
列表。添加(“+i”);
}
标题:setText(“数字”);
放射组rg=放射组;
对于(int i=0;i
}
这个解决方案实际上对我有效!。欢迎提出任何改进建议 检查此图:
您需要为底部工作表行为创建一个侦听器,并动态更改按钮位置,同时考虑底部是否由用户滑动谢谢您的输入bro,但它没有模拟我的预期行为..它显示了相同..我面临着相同的问题,你找到什么解决办法了吗?@Himanshu我已经添加了我的解决办法。@Himanshu,如果我的答案对你有帮助,请投我的一票。我也这么说,但没有用。你能复习一下这个话题吗
BottomSheetExample bottomSheet = new BottomSheetExample();
bottomSheet.showNow(this.getSupportFragmentManager(), "tag");
<?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"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:layout_marginRight="20dp"
android:text="" />
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"/>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.widget.Button
android:id="@+id/ok"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginTop="10dp"
android:gravity="bottom"
android:text="OK" />
</RelativeLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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="match_parent"
>
<LinearLayout
android:id="@+id/bottom_sheet_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:textSize="20sp"
/>
<LinearLayout
android:layout_below="@id/title"
android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="300dp">
<androidx.core.widget.NestedScrollView
android:id="@+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginBottom="10dp" />
</androidx.core.widget.NestedScrollView>
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/click_btn"
android:layout_below="@id/scroll_layout"/>
</RelativeLayout>
</LinearLayout>
public class BottomSheetExample extends BottomSheetDialogFragment {
@BindView(R.id.title)
TextView title;
@BindView(R.id.click_btn)
Button ok;
@BindView(R.id.nested_scroll_view)
NestedScrollView nestedScrollView;
@BindView(R.id.radiogroup)
RadioGroup radioGroup;
@BindView(R.id.bottom_sheet_layout)
LinearLayout bottomSheetLayout;
private BottomSheetBehavior sheetBehavior;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_sheet, container, false);
ButterKnife.bind(this, view);
sheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
sheetBehavior.setPeekHeight(R.dimen.peek_height);//put this ub dimens.xml (300dp)
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;
}