更改通知的位置

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

我在不同的设备上有两个应用程序 - Rider&Driver。一旦驱动程序接受请求,我就会从驱动程序应用程序向Rider应用程序发送通知:

private void acceptBooking(String customerId) {

    Token token = new Token(customerId);

    Map<String, String> content = new HashMap<>();
    content.put("title", "Accept");
    content.put("message", "Your request is accepted, Please make your payment!");


    DataMessage dataMessage = new DataMessage(token.getToken(), content);

    mFCMService.sendMessage(dataMessage)
        .enqueue(new Callback<FCMResponse>() {
            @Override
            public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
                if (response.body().success == 1) {
                    Toast.makeText(CustomerCall.this, "Ride Accepted",
                            Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            @Override
            public void onFailure(Call<FCMResponse> call, Throwable t) {}
        });
}

一旦acceptBooking()运行,驱动程序就会收到一个Toast“Ride Accepted”并且Rider收到一个祝酒词“你的请求被接受,请付款!”

我需要做的是将吐司放在屏幕中间的Rider App中,改变颜色和背景颜色。

我尝试了以下建议,但似乎不适用于此类通知。

android notifications position toast
1个回答
1
投票

试试这个。像这样添加一个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="@+id/custom_toast_container"
android:background="@drawable/complete_round_light_green_fill">

<TextView
    android:id="@+id/message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_marginHorizontal="24dp"
    android:layout_marginVertical="15dp"
    android:layout_gravity="center_horizontal"
    android:fontFamily="@font/ubuntu"
    android:textSize="14sp"
    android:lineSpacingExtra="7sp"
    android:textColor="@color/white"
    tools:text="abcd"
    />

</LinearLayout>

在Java代码中添加它。如您所见,我们可以添加背景,将重力设置为吐司:

LayoutInflater inflater = LayoutInflater.from(this);
    View layout = inflater.inflate(R.layout.layout_toast_green, null);

    LinearLayout customContainer = (LinearLayout) layout.findViewById(R.id.custom_toast_container);
    customContainer.setBackgroundResource(R.drawable.complete_round_mgred_fill);
    TextView text = (TextView) layout.findViewById(R.id.message);
    text.setText("Your request is accepted, Please make your payment!");

    Toast toast = new Toast(AppController.getContext());
    toast.setDuration(Toast.LENGTH_LONG);
    toast.setGravity(Gravity.CENTER,0,0);
    toast.setView(layout);
    toast.show();

complete_round_light_green_fill的Xml代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<item>
    <shape android:shape="rectangle" >
        <solid android:color="#78cd96" />
        <corners android:radius="100dip"/>
    </shape>

</item>

</layer-list>
© www.soinside.com 2019 - 2024. All rights reserved.