保持按钮对话框的底部

发布于 2025-02-13 16:00:26 字数 3787 浏览 0 评论 0原文

我有一个像设计1 这样的布局,我想总是将取消按钮与对话框的底部保持一致。 RecyClerview1&由于显示内容,RecyClerview2具有不同的高度。如果RecyClerview1和RecyClerview内容都很小,并且能够在屏幕中显示不滚动,则应该像 Design 2 。如果循环系统超过高度对话框,则应将设备高度放在对话框的底部,例如 Design 3

当前输出和最终输出

当前代码:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
        
        <TextView
            android:id="@+id/textview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:text="text1"
            android:textSize="10sp" />
        
        <TextView
            android:id="@+id/textview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@id/textview1"
            android:text="text2"
            android:textSize="10sp" />
        
        <androidx.core.widget.NestedScrollView
            android:id="@+id/scrollView"
            android:layout_below="@id/textview2"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                
                <LinearLayout
                    android:id="@+id/linearLayoutContainer1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                    
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                        
                </LinearLayout>
                
                <LinearLayout
                    android:id="@+id/linearLayoutContainer2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                    
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                
                </LinearLayout>
            
            </LinearLayout>
        
        </androidx.core.widget.NestedScrollView>
        
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/scrollView"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:gravity="right">
            
            <Button
                android:id="@+id/btnCancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel"/>
        
        </LinearLayout>
    
    </RelativeLayout>

I've a layout like design 1 and I want to align cancel button to bottom of the dialog always. recyclerview1 & recyclerview2 have different heights due to display content. if the both recyclerview1 and recyclerview content are small and able to display within screen without scroll it should be like design 2 . If the recyclerviews exceeded the heights dialog should take the device height and align cancel button to bottom of the dialog like design 3.

Current output and final output

Current code:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
        
        <TextView
            android:id="@+id/textview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:text="text1"
            android:textSize="10sp" />
        
        <TextView
            android:id="@+id/textview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@id/textview1"
            android:text="text2"
            android:textSize="10sp" />
        
        <androidx.core.widget.NestedScrollView
            android:id="@+id/scrollView"
            android:layout_below="@id/textview2"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                
                <LinearLayout
                    android:id="@+id/linearLayoutContainer1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                    
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                        
                </LinearLayout>
                
                <LinearLayout
                    android:id="@+id/linearLayoutContainer2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                    
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                
                </LinearLayout>
            
            </LinearLayout>
        
        </androidx.core.widget.NestedScrollView>
        
        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/scrollView"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:gravity="right">
            
            <Button
                android:id="@+id/btnCancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel"/>
        
        </LinearLayout>
    
    </RelativeLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

杀お生予夺 2025-02-20 16:00:26

您将不得不

使用重力底部创建一个自定义对话框()在OnViewCreated()中膨胀一个layout_bottom_button,因此膨胀的布局将始终保持在底部,

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
    );
    params.gravity = Gravity.BOTTOM;
    View bottomButtonView = this.getLayoutInflater().inflate(R.layout.layout_bottom_button, null);
    
this.getDialog().addContentView(viewM, params);

请记住,recyClerview应该具有与Layout_bottom_bottom_bottom_button Layut高度相同的底部余量。

You will have to create a custom dialog

inside onViewCreated() inflate a layout_bottom_button using gravity bottom, so inflated layout will always be at bottom

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
    );
    params.gravity = Gravity.BOTTOM;
    View bottomButtonView = this.getLayoutInflater().inflate(R.layout.layout_bottom_button, null);
    
this.getDialog().addContentView(viewM, params);

keep in mind that recyclerView should have bottom margin same as layout_bottom_button layout height, so that recyclerView does not hide below button.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文