java 如何设置Android的底部高度match_parent?

drkbr07n  于 2023-01-29  发布在  Java
关注(0)|答案(3)|浏览(181)
    • bounty将在4天后过期**。回答此问题可获得+50的声望奖励。oo-oo正在寻找规范答案

我希望底部对话框的高度扩展到match_parent(作为空活动)
这是我的代码。

    • 一月一日**
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.google.android.material.bottomsheet.BottomSheetBehavior;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button OpenBottomSheet = findViewById(R.id.open_bottom_sheet);

        OpenBottomSheet.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        BottomSheetDialog bottomSheet = new BottomSheetDialog();
                        bottomSheet.show(getSupportFragmentManager(),
                                "ModalBottomSheet");
                    }
        });
     }
}
    • 一米一米一**
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

public class BottomSheetDialog extends BottomSheetDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable
    ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View v = inflater.inflate(R.layout.buttom_sheet_layout,
                container, false);

        return v;
    }

}

Here is full code.

f0brbegy

f0brbegy1#

要获得全屏BottomSheetDialogFragment,您需要:

  • 通过重写getTheme(),使用以自定义样式应用的android:windowFullscreen来使用全屏窗口
  • 使用底部工作表的STATE_EXPANDED状态扩展到整个对话框窗口
  • 将底部工作表布局设置为MATCH_PARENT
  • 也可以使用behavior.setSkipCollapsed()禁用底部工作表STATE_HALF_EXPANDED

将其应用到您的课堂:

public class BottomSheetDialog extends BottomSheetDialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable
    ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.bottom_sheet_layout,
                container, false);

        Button algo_button = v.findViewById(R.id.algo_button);
        Button course_button = v.findViewById(R.id.course_button);

        algo_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),
                                "Algorithm Shared", Toast.LENGTH_SHORT)
                        .show();
                dismiss();
            }
        });

        course_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),
                                "Course Shared", Toast.LENGTH_SHORT)
                        .show();
                dismiss();
            }
        });
        
        return v;
    }

    @Override
    public int getTheme() {
        return R.style.dialog_style;
    }

    @Override
    public void onStart() {
        super.onStart();
        
        View bottomSheet =
                ((com.google.android.material.bottomsheet.BottomSheetDialog) 
                getDialog()).findViewById(com.google.android.material.R.id.design_bottom_sheet);

        if (bottomSheet != null) {
            // set the bottom sheet state to Expanded to expand to the entire window
            BottomSheetBehavior.from(bottomSheet).setState(BottomSheetBehavior.STATE_EXPANDED);
            // disable the STATE_HALF_EXPANDED state
            BottomSheetBehavior.from(bottomSheet).setSkipCollapsed(true);
            // set the bottom sheet height to match_parent
            ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
            bottomSheet.setLayoutParams(layoutParams);
        }

        // Make the dialog cover the status bar
        getDialog().getWindow().setFlags(
             WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
             WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

    }

}

这是全屏的自定义样式:

<resources>

    <style name="dialog_style" parent="Theme.MaterialComponents.BottomSheetDialog">
        <item name="android:windowFullscreen">true</item>
    </style>

</resources>

如果使用的是"材质3",则改为从Theme.Material3.DayNight.BottomSheetDialog扩展样式。

    • 编辑:**

太棒了!但是顶部的栏不会隐藏
您可以通过设置窗口下方标志使底部工作表隐藏状态栏;上述类别更新为:

// Make the dialog cover the status bar
getDialog().getWindow().setFlags(
     WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
     WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

同样对于带凹槽的设备,您需要将android:windowLayoutInDisplayCutoutMode设置为样式:

<resources>

    <style name="dialog_style" parent="Theme.MaterialComponents.BottomSheetDialog">
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    </style>

</resources>
qij5mzcb

qij5mzcb2#

通过将以下代码行添加到BottomSheetDialog类中,可以将BottomSheetDialog的高度设置为match_parent:

@Override
public void onStart() {
    super.onStart();
    View view = getView();
    if (view != null) {
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        view.setLayoutParams(params);
    }
}

此代码重写onStart()方法,该方法在显示BottomSheetDialog时调用。它获取BottomSheetDialog的视图,将视图的高度设置为
或者,您可以在onCreateView方法中使用此行:

getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

这一行将底部工作表对话框的宽度和高度设置为match_parent,使其占据整个屏幕。

zc0qhyus

zc0qhyus3#

有一个对我有用的技巧方法,可以修改行为和设置窗口高度以实现全屏效果。

class BottomSheetDialog : BottomSheetDialogFragment() {
    
    private var mBehavior: BottomSheetBehavior<View>? = null

    override fun onStart() {
        super.onStart()
        mBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
        mBehavior?.peekHeight = 0
    }
    
    fun onCreateView(
        inflater: LayoutInflater,
        @Nullable container: ViewGroup?,
        @Nullable savedInstanceState: Bundle?
    ): View {
        //content in buttom_sheet_layout should be match_parent
        return inflater.inflate(
            R.layout.buttom_sheet_layout,
            container, false
        )
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupBottomSheetFullScreen(view)
    }

    private fun setupBottomSheetFullScreen(view: View) {
        view.updateLayoutParams {
            height = getWindowHeight()
        }
        mBehavior = BottomSheetBehavior.from(view.parent as View)
    }

    private fun getWindowHeight(): Int {
        return Resources.getSystem().displayMetrics.heightPixels
    }
}

相关问题