Как получить тост Roundend - PullRequest
1 голос
/ 08 января 2020

Я сделал тост следующим образом:

В настоящее время показывает тост, как показано ниже -

Showed toast

Toast toast  = Toast.makeText(MainActivity.this,"Row Inserted.",Toast.LENGTH_SHORT);
  View view = toast.getView();
  view.setBackgroundColor(COlor.GREEN);
  toast.show();

Но я хочу сделать что-то вроде этого:

I want to do this:

Ответы [ 3 ]

1 голос
/ 08 января 2020

создайте фигуру в рисованной форме следующим образом:

<?xml version="1.0" encoding="UTF-8"?>

<gradient
    android:angle="90"
    android:endColor="#FFC107"
    android:startColor="#FFC107"/>
<corners
    android:radius="40dp"/>

и затем получите доступ к ней следующим образом:

Toast toast  = Toast.makeText(AppMenusActivity.this,"Row Inserted.",Toast.LENGTH_SHORT);
    View view = toast.getView();
    view.setBackground(getResources().getDrawable(R.drawable.btngradient));
    toast.show();
0 голосов
/ 08 января 2020

Вот самый простой и точный способ -

Сначала вызовите этот метод в своем java коде -

private void toastIconSuccess() {
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);

        //inflate view
        View custom_view = getLayoutInflater().inflate(R.layout.toast_icon_text, null);
        ((TextView) custom_view.findViewById(R.id.message)).setText("Success!");
        ((ImageView) custom_view.findViewById(R.id.icon)).setImageResource(R.drawable.ic_done);
        ((CardView) custom_view.findViewById(R.id.parent_view)).setCardBackgroundColor(getResources().getColor(R.color.green_500));

        toast.setView(custom_view);
        toast.show();
    }

И затем создайте этот пользовательский макет - с именем toast_icon_text в папке макета -

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/parent_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    app:cardBackgroundColor="#263238"
    app:cardCornerRadius="23dp"
    app:cardElevation="0dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingLeft="18dp"
        android:paddingRight="18dp"
        android:paddingTop="10dp">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="18dp"
            android:layout_height="18dp"
            android:tint="@android:color/white"
            app:srcCompat="@drawable/ic_close" />

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:maxLines="1"
            android:singleLine="true"
            android:text="Error Toast"
            android:textColor="#f2f2f2" />

        <View
            android:layout_width="10dp"
            android:layout_height="0dp" />

    </LinearLayout>

</android.support.v7.widget.CardView>

Изменение цвета и значков по вашему выбору.

0 голосов
/ 08 января 2020

Вы можете сделать собственный тост. Пожалуйста, обратитесь к приведенному ниже примеру кода

Это java код.

LayoutInflater inflater = SelectCyclesActivity.this.getLayoutInflater();
        View layout = inflater.inflate(R.layout.layout_toast,
                (ViewGroup) SelectCyclesActivity.this.findViewById(R.id.custom_toast_container));

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setSimpleCustomText(sMessage, Constants.SECOND_FONT);

        Toast toast = new Toast(SelectCyclesActivity.this);
        toast.setGravity(Gravity.BOTTOM, 0, 60);

        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();

Это XML файл layout_toast. xml

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


    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/_10sdp"
        android:layout_marginRight="@dimen/_10sdp"
        android:padding="@dimen/_12sdp"
        android:gravity="center"
        android:textColor="#FFF"
        android:textStyle="bold" />

</LinearLayout>

Это округлая форма для фона тост. toast_bg. xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:width="@dimen/_2sdp"
        android:color="@color/colordrakGray" />
    <corners android:radius="30dp" />
</shape>
...