【问题标题】:android: custom snackbar bar displays default snack bar in the backgroundandroid:自定义小吃店在后台显示默认小吃店
【发布时间】:2020-05-18 21:28:59
【问题描述】:

我有一个自定义布局的自定义小吃店。但它无法正确显示。它还在后台显示默认小吃店:下面是我的代码:

    import android.support.annotation.NonNull;
    import android.support.design.widget.BaseTransientBottomBar;
    import android.support.design.widget.Snackbar;
    import android.support.v7.widget.AppCompatTextView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class CustomSnackbar extends BaseTransientBottomBar<CustomSnackbar> {

    protected CustomSnackbar(@NonNull ViewGroup parent,
                             @NonNull View content, @NonNull 
        android.support.design.snackbar.ContentViewCallback contentViewCallback) {
        super(parent, content, contentViewCallback);
    }

    private static class ContentViewCallback
            implements android.support.design.snackbar.ContentViewCallback {

        // view inflated from custom layout
        private View view;

        public ContentViewCallback(View view) {
            this.view = view;
        }

        @Override
        public void animateContentIn(int delay, int duration) {
            // TODO: handle enter animation
        }

        @Override
        public void animateContentOut(int delay, int duration) {
            // TODO: handle exit animation
        }
    }

    public static CustomSnackbar make(ViewGroup parent, int duration,String text) {
        // inflate custom layout
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.custom_snackbar, parent, false);
        //Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout)view;
        //layout.setPadding(0, 0, 0, 0);//set padding to 0
        AppCompatTextView textView = (AppCompatTextView) view.findViewById(R.id.textview_snackbar_text);
        textView.setText(text);
        // create with custom view
        ContentViewCallback callback= new ContentViewCallback(view);
        CustomSnackbar customSnackbar = new CustomSnackbar(parent, view, callback);
        customSnackbar.setDuration(duration);
        return customSnackbar;
    }
}

以下是我的自定义小吃栏的自定义布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="10dp"
android:padding="0dp"
android:background="@android:color/transparent"
>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/button_green"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    >
<android.support.v7.widget.AppCompatTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textview_snackbar_text"
    android:textColor="@color/white"
    android:text=""
    android:textSize="@dimen/extra_small_text_size"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    />
</LinearLayout>
</android.support.v7.widget.CardView>

这就是我在 BaseActivity 中使用自定义小吃栏的方式:我在 BaseActivity 和 android.R.id.content 中使用它,以便我可以从所有活动中调用我的自定义小吃栏。

public void showSnackbar(String message) {
        CustomSnackbar.make(findViewById(android.R.id.content),
                CustomSnackbar.LENGTH_LONG,message).show();
}

但是当我调用这个方法时,它是这样显示的:

我探索了许多堆栈溢出问题,例如 Custom Snackbar doesn't work properlySnackbar from custom class not showing,但这些解决方案对我不起作用。

任何想法或见解都会非常有帮助。

Edit1:如给出的答案中所述,;

在我的 CustomSnackbar 的 make 方法中:我给出了以下内容:

    view.setBackgroundColor(parent.getContext().getResources().getColor(R.color.design_snackbar_background_color));

它看起来像下面这样:

我的cardview的行为现在丢失了:有什么办法可以恢复我的cardview的圆角半径以及要拉伸到最大的宽度!!??

【问题讨论】:

  • 现在检查编辑的答案

标签: android android-custom-view android-snackbar


【解决方案1】:

覆盖以下值

<color name="design_snackbar_background_color" tools:override="true">@color/transparent</color>

    Snackbar snackbar = Snackbar.make((ViewGroup) findViewById(android.R.id.content), "", Snackbar.LENGTH_LONG);
    Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
    TextView textView = (TextView) layout.findViewById(android.support.design.R.id.snackbar_text);
    textView.setVisibility(View.INVISIBLE);
    LayoutInflater mInflater = LayoutInflater.from(getApplicationContext());
    View snackView = mInflater.inflate(R.layout.snackbar, null);
    TextView textViewTop = (TextView) snackView.findViewById(R.id.textview_snackbar_text);
    textViewTop.setText("ssdfds");
    textViewTop.setTextColor(Color.WHITE);
    layout.setPadding(0,0,0,0);
    layout.addView(snackView, 0);
    layout.setBackgroundColor(getResources().getColor(R.color.transparent));
    snackbar.show();

在你的styles.xml中粘贴下面的代码

 <style name="MyRoundSnackbar" parent="@style/Widget.MaterialComponents.Snackbar">
        <item name="android:layout_margin">32dp</item>
    </style>

然后在你的主题中粘贴这一行

<item name="snackbarStyle">@style/MyRoundSnackbar</item>

将此行粘贴到您的 xml 中

<color name="transparent">#00FFFFFF</color>

【讨论】:

  • 我无法从我的 CustomSnackbar 类中调用 setBackgroundColor !
  • 感谢您的意见。它确实有效!你能解释一下,你到底做了什么,因为当我尝试 textview 的可见性为 GONE 而不是 INVISIBLE 时,即使它在异常块下它也会崩溃!
  • 好吧,代码本身是不言自明的。我们正在切换其可见性的文本视图是快餐栏的默认文本。但是当我们想要扩展我们自己的布局时,我们需要让它不可见并且不会消失,因为它是内置的文本。之后我刚刚添加了背景颜色和边距。如果您认为此答案解决了您的问题,请接受答案。
  • 感谢您的回复: 1.只是对崩溃感到好奇:我已在 try{ tv.setVsisibility(GONE); 中将内置 textview 设置为 GONE }catch(Exception e){}:但它甚至没有进入 catch 块,并且在执行 try 后就崩溃了:为什么会这样? 2.那么哪个选项更好?一个定制的小吃店还是你给的充气的?
  • 3.现在,我无法显示我的默认小吃栏:我尝试删除所有视图,但它不起作用:如何也显示我的默认小吃栏,例如基于 if(custom)else{默认}条件?能否请您提供一些见解
【解决方案2】:

Try this library 使用像 Gmail 这样的材料浮动快餐栏所有功能,如添加自定义图像也可用。

【讨论】:

    【解决方案3】:

    使用cardview后面的透明背景。像这样:

        <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/_8sdp"
        android:background="@android:color/transparent">
    
        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="@dimen/_8sdp"
            app:cardBackgroundColor="@color/bg_snackbar">
    
            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/pLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="@dimen/_4sdp"
                android:paddingBottom="@dimen/_4sdp"
                android:paddingStart="@dimen/_4sdp">
    
                <ImageView
                    android:id="@+id/image"
                    android:layout_width="@dimen/_30sdp"
                    android:layout_height="@dimen/_30sdp"
                    android:scaleType="centerInside"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:srcCompat="@drawable/ic_outline_info_24" />
    
                <TextView
                    android:id="@+id/message"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/_12sdp"
                    android:layout_marginEnd="@dimen/_12sdp"
                    android:ellipsize="end"
                    android:justificationMode="inter_word"
                    android:lineSpacingExtra="@dimen/_4sdp"
                    android:maxLines="2"
                    android:textColor="#fff"
                    android:textSize="@dimen/_8sdp"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/image"
                    app:layout_constraintStart_toEndOf="@+id/view"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:text="@string/lorem" />
    
                <TextView
                    android:id="@+id/action"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    tools:text="سبد خرید"
                    android:textColor="#fff"
                    android:fontFamily="serif"
                    android:gravity="center"
                    android:textSize="@dimen/_10sdp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
    
                <View
                    android:id="@+id/view"
                    android:layout_width="1dp"
                    android:layout_height="@dimen/_25sdp"
                    android:layout_alignParentBottom="true"
                    android:layout_marginStart="@dimen/_8sdp"
                    android:background="@color/grey_100"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/action"
                    app:layout_constraintTop_toTopOf="parent" />
    
            </androidx.constraintlayout.widget.ConstraintLayout>
    
        </com.google.android.material.card.MaterialCardView>
    
    </androidx.coordinatorlayout.widget.CoordinatorLayout>
    

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 2016-03-13
      • 2018-05-04
      • 2020-07-25
      • 2021-07-10
      • 2020-05-22
      • 2020-04-07
      • 2022-10-08
      • 2018-09-27
      相关资源
      最近更新 更多