简体   繁体   English

显示文本视图的冗长内容作为Toa​​st消息android

[英]Show lengthy content of a textview as a toast message android

I have a TextView and it contains lengthy contents. 我有一个TextView,它包含冗长的内容。 These textview are readonly and no scrolling provided. 这些textview是只读的,不提供滚动。 So that entire text cannot be seen in the textview. 这样就无法在textview中看到整个文本。 Now I want to show a toast message in the longClick event. 现在,我想在longClick事件中显示一条祝酒消息。 But the toast message is shown in the bottom of the screen. 但是,吐司消息显示在屏幕底部。 How can I show it just below the selected TextView? 如何在选定的TextView下方显示它?

final Toast viewToast = Toast.makeText(getActivity(), packageId.getText().toString(), Toast.LENGTH_LONG);
    packageId.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            viewToast.show();
            return false;
        }
    });`

You can show the Toast at different location by using this method: 您可以使用以下方法在不同位置显示Toast:

customToast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

For custom location please go through this URL 对于自定义位置,请通过此URL

For custom position please go through this link 对于自定义职位,请通过此链接

How can I show it just below the selected TextView? 如何在选定的TextView下方显示它?

To show Toast below of TextView or any View use Toast.setGravity method with X and Y offsets: 要在TextView或任何视图的下方显示Toast,请使用具有X和Y偏移量的Toast.setGravity方法:

int coordinates[] = new int[2];
textView.getLocationInWindow(coordinates);
int xOffset=coordinates[0]+getWidth()/2;
int yOffset=coordinates[1]+ getHeight;
viewToast.setGravity(Gravity.TOP|Gravity.LEFT,xOffset,yOffset);
viewToast.show();

Change position of Toast on Activity Window using xOffset and yOffset according to your requirement. 根据需要,使用xOffsetyOffset更改“活动”窗口上Toast的位置。

Try make your toast custom, 尝试让您的吐司风俗,

<ImageView android:id="@+id/image1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="10dp" />

<TextView android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF" />

Java code Java代码

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

ImageView image = (ImageView) layout.findViewById(R.id.image1);
image.setImageResource(R.drawable.android);
TextView text = (TextView) layout.findViewById(R.id.text1);
text.setText("Type your long message here");

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM