【问题标题】:Dynamically modify layout of a toast Android动态修改 Toast Android 的布局
【发布时间】:2016-12-07 04:20:27
【问题描述】:

我在 Android Studio 中创建应用时遇到问题,我自己解释一下

我为我的 toast 创建了一个布局,XML 看起来像这样

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/elemento_correcto_s" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:background="#afff45"
android:weightSum="1">
<ImageView android:layout_height="100dp" android:layout_width="100dp" android:src="@drawable/base" android:padding="5dip" android:id="@+id/ok"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false">
</ImageView>
<TextView android:layout_height="50dp" android:id="@+id/tv" android:layout_width="match_parent" android:text="¡CORRECTO!" android:textColor="#FFF" android:gravity="center_vertical|center_horizontal"
    android:layout_gravity="center_vertical"
    android:layout_toRightOf="@+id/ok"
    android:layout_marginTop="26dp"
    >
</TextView>

当我膨胀布局时它工作正常

LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(
            R.layout.elemento_correcto_s
            , (ViewGroup) findViewById(R.id.elemento_correcto_s));
    this.elementoCorrecto = new Toast(this);
    this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0);
    this.elementoCorrecto.setDuration(Toast.LENGTH_LONG);
    this.elementoCorrecto.setView(layout);
    this.elementoCorrecto.show();

但问题是我想动态更改 TextView 的文本,我已经尝试调用 TextView 并更改文本,但它不起作用,所以我希望你能帮帮我

这是我的代码

LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(
            R.layout.elemento_correcto_xl
            , (ViewGroup) findViewById(R.id.elemento_correcto_xl));

    TextView tvCombo = (TextView) findViewById(R.id.tv);
    if(combo > 1) {
        tvCombo.setText("¡" + combo + " VECES SEGUIDAS!");
    }
    else
        tvCombo.setText("¡CORRECTO!");

    this.elementoCorrecto = new Toast(this);
    this.elementoCorrecto.setGravity(Gravity.TOP, 0, 0);
    this.elementoCorrecto.setDuration(Toast.LENGTH_LONG);
    this.elementoCorrecto.setView(layout);
    this.elementoCorrecto.show();

【问题讨论】:

    标签: android android-layout android-studio layout toast


    【解决方案1】:

    你可以使用这个方法来作为我传递字符串来显示..

    public static void makeToast(Context c, String msgToShow){
        LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v= inflater.inflate(R.layout.view_toast,null); //your layout to show
        TextView text = (TextView) v.findViewById(R.id.textViewToast);
        text.setText(msgToShow);
        Toast toast = new Toast(c);
        toast.setGravity(Gravity.BOTTOM, 10, 25);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(v);
        toast.show();
    }
    

    在活动中的任意位置调用它。

    【讨论】:

    • 好人!你真的理解我的问题,我试过了,它有效!谢谢
    【解决方案2】:

    使用 TextView 显示敬酒是否有任何特殊原因?否则,您可以按照以下方法动态显示 toast -

     String toastText = "";
     if(combo > 1) {
        toastText = "¡" + combo + " VECES SEGUIDAS!";
    }
    else
        toastText = "¡CORRECTO!";
    Toast.makeText(activity, toastText, Toast.LENGTH_SHORT).show();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2012-12-04
      相关资源
      最近更新 更多