如何获得圆面包

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

我像这样烤面包:

当前正在像下面显示吐司-

“显示吐司”

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

但是我想做这样的事情:

“我想这样做:”

android android-toast
2个回答
0
投票

您可以定制吐司。请参阅下面的示例代码

这是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>

0
投票

这是最简单且更精确的-

首先,在您的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>

更改颜色和图标作为您的选择。

© www.soinside.com 2019 - 2024. All rights reserved.