Android Studio 底部工作表使我无法与背景(后面)上的对象进行交互

问题描述 投票:0回答:1

我有一个 XML 布局,我使用

BottomSheetDragHandleView
作为底部工作表,但是当它显示时我根本无法访问任何内容。我应该怎么办?我目前在我的主要活动 XML 布局中使用坐标布局。

这是我的Java代码:

package com.example.outletmapsbeta;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.button.MaterialButton;

public class MainActivity extends AppCompatActivity {

    Button butt_test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        BottomSheetDialog bottomSheetDialogIm3 = new BottomSheetDialog(this);
        BottomSheetDialog bottomSheetDialog3id = new BottomSheetDialog(this);

        // Inflate layout XML
        LayoutInflater inflater = LayoutInflater.from(this);
        View bottomSheetViewIm3 = inflater.inflate(R.layout.bottom_sheet_layout_im3, null);

        // Set the view for the BottomSheetDialog
        bottomSheetDialogIm3.setContentView(bottomSheetViewIm3);

        bottomSheetDialogIm3.getWindow().setDimAmount(0f); // 0f berarti tidak ada dimming
        bottomSheetDialogIm3.setCanceledOnTouchOutside(false);
        BottomSheetBehavior<View> behaviorIm3 = BottomSheetBehavior.from((View) bottomSheetViewIm3.getParent());

        // Set the peek height
        behaviorIm3.setPeekHeight(1045); // Set this to whatever height you want in pixels


        View bottomSheetView3id = inflater.inflate(R.layout.bottom_sheet_layout_3id, null);

        // Set the view for the BottomSheetDialog
        bottomSheetDialog3id.setContentView(bottomSheetView3id);

        bottomSheetDialog3id.getWindow().setDimAmount(0f); // 0f berarti tidak ada dimming
        bottomSheetDialog3id.setCanceledOnTouchOutside(false);
        BottomSheetBehavior<View> behavior3id = BottomSheetBehavior.from((View) bottomSheetView3id.getParent());

        // Set the peek height
        behavior3id.setPeekHeight(1045); // Set this to whatever height you want in pixels



        // Tampilkan Bottom Sheet saat button diklik
        Button showBottomSheetButtonim3 = findViewById(R.id.butt_test_im3);
        showBottomSheetButtonim3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialogIm3.show();
            }
        });

        Button showBottomSheetButton3id = findViewById(R.id.butt_test_3id);
        showBottomSheetButton3id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomSheetDialog3id.show();
            }
        });

        Button closeButton = findViewById(R.id.butt_test_close);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bottomSheetDialogIm3.isShowing() || bottomSheetDialog3id.isShowing()) {
                    bottomSheetDialogIm3.dismiss();
                    bottomSheetDialog3id.dismiss();
                }
            }
        });
    }
}

我正在尝试制作一些类似于 Google 地图应用程序的东西。

背景中的可点击对象(后面)。

java android-studio bottom-sheet android-bottomsheetdialog
1个回答
0
投票

我正在尝试制作一些类似于 Google 地图应用程序的东西。

背景中的可点击对象(后面)。

如果问题根本无法与背景交互,则

BottomSheetBehavior
可能就是您所需要的。你的 .xml 文件中会是这样的

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
                <LinearLayout
                    android:id="@+id/bottom_sheet"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:animateLayoutChanges="true"
                    android:gravity="center_horizontal"
                    android:orientation="vertical"
                    app:behavior_hideable="true"
                    app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
                    app:layout_constraintBottom_toBottomOf="parent">

               <!-- Contents of your bottom sheet go here -->
               </LinearLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

然后在您的活动中您可以像这样:

LinearLayout bottomSheet = requireView().findViewById(R.id.bottom_sheet);
BottomSheetBehavior<LinearLayout> bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
    
// showing the bottom sheet
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
// hiding the bottom sheet
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
bottomSheetBehavior.setPeekHeight(0);
© www.soinside.com 2019 - 2024. All rights reserved.