【问题标题】:How to customize full toast size in android如何在android中自定义完整的吐司大小
【发布时间】:2013-09-14 18:18:35
【问题描述】:

我正在展示一个定制的吐司,带有一些生物的背景图像,但我希望吐司具有我定义的大小。

在普通吐司中使用这个是不可能的:

View view = toast.getView();
view.setBackgroundResource(R.drawable.go);
toast.setView(view);`

然后我像这样使用布局充气器:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.hit_toast,
                                    (ViewGroup) findViewById(R.id.custom_toast_layout));
     layout.setBackgroundResource(R.drawable.go);
     TextView text = (TextView) layout.findViewById(R.id.textToShow);
     // Set the Text to show in TextView
     text.setText(msg);

     Toast toast = new Toast(getApplicationContext());
     //toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
     toast.setDuration(Toast.LENGTH_SHORT);
     toast.setView(layout);
     toast.show();

和我的 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:background="#DAAA"
    android:gravity="center"
    android:orientation="horizontal"
    android:padding="8dp" >

    <TextView
        android:id="@+id/textToShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="ll"
        android:textColor="#00ffff" />
</LinearLayout>`

但吐司的大小仍然相同,只是位置发生了变化。

【问题讨论】:

    标签: android toast


    【解决方案1】:

    如果您想自定义吐司,那么SuperToast library 就是答案。

    在定制方面灵活而无限!

    【讨论】:

      【解决方案2】:

      我尝试改变尺寸,即宽度和高度。但似乎你永远无法显示指定宽度和高度的吐司......吐司会根据内容改变形状,比如背景图像或里面的文字...... android 中提供的默认 toast 也是如此。

      您可以随时创建一个名为toast_layout 的小布局并将其可见性设置为不可见,而不是这样做。在您要显示的同一布局文件中创建它。将它放在您希望吐司出现的任何地方。

      <RelativeLayout
              android:id="@+id/toast_layout"
              android:layout_width="fill_parent"
              android:layout_height="100dp"
              android:layout_alignParentBottom="true"
              android:layout_alignParentLeft="true"
              android:background="@drawable/toastbackground"
              android:visibility="invisible" >
      
              <ImageView
                  android:id="@+id/toast_img"
                  android:layout_width="100dp"
                  android:layout_height="100dp"
                  android:layout_alignParentLeft="true"
                  android:layout_centerVertical="true"
                  android:src="@drawable/man" />
      
              <TextView
                  android:id="@+id/toast_text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_centerVertical="true"
                  android:layout_toRightOf="@+id/down_img"
                  android:text="Large Text"
                  android:textAppearance="?android:attr/textAppearanceMedium"
                  android:textColor="#00ffff"
                  android:textStyle="bold|italic" />
      
          </RelativeLayout>
      

      在 res/anim 文件夹中创建一个名为 fade_in_out 的 tweenAnimation 文件,这里与普通 toast 不同,您可以根据需要自定义淡入淡出和淡出属性。

      然后,当您想在活动中显示 toast 时调用以下函数,例如:-

      clone_toast(R.drawable.image,"Yao it Works...");
      
      void clone_toast(int ids, String msg) {
              toast_img.setImageResource(ids);
              toast_text.setText(msg);
              toast_layot.setVisibility(View.VISIBLE);
              toast_layout.startAnimation(fade_in_out);
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-02
        相关资源
        最近更新 更多