Android 尝试扩充自定义布局时,Snackbar 未占据全宽

Android Snackbar does not take full width when trying to inflate custom layout

我正在尝试使用我的自定义布局更改快餐栏,但自定义布局未占据 snackbar.Why 的全宽?

这是我的代码:-

        CoordinatorLayout snackbarCoordinatorLayout;

        snackbarCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.snackbarCoordinatorLayout);
        RelativeLayout objRelativeLayout = new RelativeLayout(mContext);
        RelativeLayout.LayoutParams objLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        // Create the Snackbar
        final Snackbar snackbar = Snackbar.make(snackbarCoordinatorLayout, "", Snackbar.LENGTH_INDEFINITE);
        // Get the Snackbar's layout view
        Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();

        TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
        textView.setVisibility(View.INVISIBLE);

        LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View snackView = objLayoutInflater.inflate(R.layout.snackbar_layout, null);// use the parent ViewGroup instead of null        // Configure the view

        Button btnInvite = (Button) snackView.findViewById(R.id.btn_invite_snackbar);
        Button btnCancel = (Button) snackView.findViewById(R.id.btn_close_snackbar);

        btnInvite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                snackbar.dismiss();
                launchShareActivity();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                snackbar.dismiss();
            }
        });
        snackView.setLayoutParams(objLayoutParams);
        layout.addView(snackView, 0);
        snackbar.show();

这里是snackbar_layout.xml和协调器布局文件内容:-

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="#000">

        <Button
            android:id="@+id/btn_invite_snackbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="10dp"
            android:background="@drawable/rounded_bg"
            android:includeFontPadding="false"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="@string/invite_your_friends_to_studyboard"
            android:textAllCaps="false"
            android:textColor="@color/white" />

        <Button
            android:id="@+id/btn_close_snackbar"
            android:layout_width="45dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:background="@android:color/transparent"
            android:gravity="center_horizontal|center_vertical"
            android:text="X"
            android:textColor="@color/white"
            android:textSize="22sp" />
    </RelativeLayout>
<!--Cordinator Layout used-->
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/snackbarCoordinatorLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
<android.support.v4.view.ViewPager
                    android:id="@+id/home_view_pager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@null">

                </android.support.v4.view.ViewPager>

        </android.support.design.widget.CoordinatorLayout>

这是图像中显示的输出:-

请在您的代码中添加以下行:-

layout.setPadding(0, 0, 0, 0); //set padding to 0

你可以参考这个link的代码:-