【问题标题】:How to get android Toast Height before display如何在显示前获取android Toast Height
【发布时间】:2023-03-29 21:11:02
【问题描述】:

我一直在尝试获取自定义 toast 的高度,以便可以使用以下代码在按钮的右上角显示 toast:

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MyToast {

    public static void showToast(Context context, View v, String s) {

        int pos[] = new int[2];
        v.getLocationOnScreen(pos);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View layout = inflater.inflate(R.layout.toast_style, (ViewGroup) v.findViewById(R.id.toast_layout_root));

        TextView text = (TextView) layout.findViewById(R.id.toast_text);
        text.setText(s);

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.TOP|Gravity.LEFT, pos[0] + v.getWidth(), pos[1] - v.getHeight());
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(layout);
        toast.show();
    }
}

我通过了按钮的视图,所以我可以得到我想要放置吐司的位置,但是 v.getHeight() 不是完成这项任务所需的高度,有什么建议吗?

这是xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8dp"
    android:background="@drawable/frame_outline" >

    <TextView
        android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:textStyle="italic"
        android:textSize="12sp"
        android:typeface="serif"
        android:gravity="center"
        android:textColor="@color/holo_text" />

</LinearLayout>

【问题讨论】:

  • 我尝试使用 text.getHeight() 和 layout.getHeight() 但两者都为零
  • 方法何时被调用?也许视图还没有绘制?
  • 我不能检索正在创建的视图的高度吗?它不应该显示有高度
  • @Raddy 发布您的 xml
  • 即使自从我上次使用 Android 开发以来已经过去了很多时间,我确信 Android 不会向您报告 View 的高度/宽度,直到它没有绘制它。如果没有显示,Android 不会绘制视图,因为没用。

标签: android android-toast


【解决方案1】:

您可以使用ViewTreeObserver 获取textView 的高度而不显示在屏幕上,通过调用其getViewTreeObserver 方法并获取ViewTreeObserveronGlobalLayout 方法内部的高度。

样本:

final TextView text = (TextView) layout.findViewById(R.id.toast_text);
          text.setText(s);
          final ViewTreeObserver observer= text.getViewTreeObserver();
           observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                 Log.d("Hieght", ""+text.getHeight());   
                }
            });
         Toast

编辑:

需要使用textView的post方法同步记录高度

  final TextView text = (TextView) layout.findViewById(R.id.toast_text);
          text.setText(s);
          final ViewTreeObserver observer= text.getViewTreeObserver();
           observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                 hieght = text.getHeight();   
                }
            });
  text.post(new Runnable() {

        @Override
        public void run() {
             Log.d("Hieght", ""+hieght); //hieght is the variable you used to get the height in the onGlobalLayout

        }
    });

【讨论】:

  • 似乎工作正常,值应该是正确的,但我无法从 onGlobalLayout() 函数中获取高度,我该怎么做?我尝试在函数外部创建一个变量,但无法存储它
  • 我使用了一个静态变量,并且我能够获得高度值,但由于某种原因之前没有,无论如何我尝试通过在文本中使用换行符为吐司输入不同的高度,但它没有t 给出正确的位置,Toast 漂浮了一段距离,给我的高度与 toast 的高度不匹配,加上第一个按钮按下总是给出 height = 0
  • @Raddy 吐司内有一些填充物,所以你不会真正得到正确的高度,你需要从吐司中添加顶部和底部填充物
  • 哦,好吧,我明白你的意思了,但是如何将 8dp 转换为像素?我使用的是三星 Galaxy SII 所以 480x800
  • 好的,我使用了 24,它给出了正确的结果,但仍然存在一个问题,在第一个按钮按下时高度始终为零
猜你喜欢
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多