【问题标题】:My custom Android toast shows no text我的自定义 Android 吐司不显示任何文字
【发布时间】:2014-10-19 19:12:02
【问题描述】:

聪明人!

这是我在这里的第一个问题。我遇到了一个对我来说似乎很容易解决的问题。我无法在我的 Android 应用中显示自定义 Toast 消息。

我创建了两个自定义布局

  1. my_toast.xml - 布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_height="wrap_content">
    
    <TextView
    android:id = "@+id/text"
    android:background="@color/yumist_red"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:textColor="#fff"
    android:text = "ToastText"
    android:gravity = "center"
       />

</LinearLayout>
  1. my_toast_up.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_height="wrap_content">
    
    
    <ImageView
        android:layout_width="30dp"
        android:layout_height="10dp"
        android:src="@drawable/chat_arrow" 
        android:rotation="180"
        />
    
    <TextView
    android:id = "@+id/text"
    android:background="@color/yumist_red"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="10dp"
    android:paddingBottom="10dp" 
    android:paddingLeft="20dp" 
    android:paddingRight="20dp" 
    android:textColor="#fff"
    android:text = "ToastText"
    android:gravity = "center"
       />

</LinearLayout>

两者的唯一区别是一个包含箭头图像的图像视图。我正在尝试创建文本气泡样式的 Toast。

我能够在我的应用中展示和使用第一个。但是,当我对图像使用第二个布局时,我看到的只是图像和 ImageView 宽度和标准高度的空 TextView。我在网上尝试了很多帖子和现有问题,但我找不到解决方案。

有什么帮助吗?

[显示Toasts的java代码]

	public static void showToast(String message, int type)
	{ //'message' is the text to display, 'type' determines which of the three toasts is shown
      
      /*
      * There are three fixed toasts:
      * 1. One pointing upwards (with an angle) and placed near the top. (upperNotification)
      * 2. Placed in the default position, to show general messages. (no issues with this | mToast)
      * 3. Placed to point at bar at the bottom. (lowerNotification)
      */
      
      
		TextView textView = null ;            //this will refer to the message TextView 
                                              //corresponding to the toast selected
		Toast toast = null;                   //to refer to the toast to display
		switch(type)
		{
		case MyToast.ARROW_DOWN:textView = lowerToastText; toast = lowerNotification; break;

		case MyToast.ARROW_UP:textView = upperToastText; toast= upperNotification; break;

		case MyToast.ARROW_NONE:textView = toastText; toast = mToast; break;
		}
		if(textView != null && toast != null)
		{
			
			System.out.println(message);
			textView.setText(message);
			toast.show();
		}
		else
		{
			System.out.println("NULL!");
		}
	}

//Below is the code I used to init these.

	public static void setUpToast(Activity activity, LayoutInflater inflater)
	{
		mToast = new Toast(activity);
		upperNotification = new Toast(activity);
		lowerNotification = new Toast(activity);
		
		View layout = inflater.inflate(R.layout.my_toast, null);
		toastText = (TextView) layout.findViewById(R.id.text);
		mToast.setView(layout);
		mToast.setDuration(Toast.LENGTH_LONG);
		
		layout = inflater.inflate(R.layout.my_toast_up, null);
		upperToastText = (TextView) layout.findViewById(R.id.text);
		upperNotification.setView(layout);
		upperNotification.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, Dish.dpToPx(96, activity));
		upperNotification.setDuration(Toast.LENGTH_LONG);
		
		layout = inflater.inflate(R.layout.my_toast_down, null);
		lowerToastText = (TextView) layout.findViewById(R.id.text);
		lowerNotification.setView(layout);
		lowerNotification.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, Dish.dpToPx(48, activity));
		lowerNotification.setDuration(Toast.LENGTH_LONG);
		
	}

【问题讨论】:

  • 请分享您用来显示吐司的代码

标签: android xml user-interface styling android-toast


【解决方案1】:

我自己通过反复试验找到了解决办法。

在我的布局中,我保留了 TextView 的宽度以匹配其父级。由于某种原因,这导致它缩小了 ImageView 的宽度。

我将其更改为 WRAP_CONTENT 并将父视图更改为 MATCH_PARENT,它解决了我的问题。

不过,我仍然很好奇是否有办法以父级的宽度显示文本。

【讨论】:

  • 将父布局从线性布局改为相对布局
  • 谢谢@3amoura!我会尝试实现这一点。 (Y)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 2014-05-13
  • 1970-01-01
相关资源
最近更新 更多