自定义toast时,有些时候不能设置想要的宽和高(代码动态设置或使用文末方式)

来源:互联网 发布:seo课程多少钱 编辑:程序博客网 时间:2024/06/10 13:15

应用场景:自定义一个toast,主要设置toast.setview(view)中的view从布局xml来。

需要动态改变toast的宽高。

public static void showToast(Activity act, String str, int length) {LayoutInflater inflater = act.getLayoutInflater();View layout = inflater.inflate(R.layout.toast_submit, null);TextView text = (TextView) layout.findViewById(R.id.tv_toast_txt0);text.setText(str);if (sToast == null) {sToast = new Toast(act.getApplicationContext());}WindowManager windowManager = act.getWindowManager();   Display display = windowManager.getDefaultDisplay();   int screenWidth = display.getWidth();   int screenHeight = display.getHeight();   Log.e(TAG, "screenWidth:"+screenWidth+ "  screenHeight:"+screenHeight);   LinearLayout layout2 = (LinearLayout)layout.findViewById(R.id.ll_toast);   layout2.getLayoutParams().width = screenWidth;   layout2.getLayoutParams().height = screenHeight;sToast.setGravity(Gravity.CENTER, 0, 0);sToast.setDuration(length);sToast.setView(layout);sToast.show();}
如果是直接设置父控件的宽高,
layout.getLayoutParams().width = screenWidth;layout.getLayoutParams().height = screenHeight;
会发现报空指针异常。原因应该是这个toast还没有生成,<span style="background-color: rgb(240, 240, 240);">getLayoutParams()方法得到的是layout的父控件即toast的对象。这个对象为空</span>


最开始使用对layout使用这个setLayoutParams(new LayoutParams(width, height))方法设置不起作用,但不报错,也是因为setLayoutParams设置的是toast的宽高,而这时候toast还没有生成,所以没效果。


所以以后要改变这个自定义toast的大小,必须设置layout子控件layout2的大小才能达到这个效果。

附上xml布局文件 R.layout.toast_submit

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="horizontal"     android:background="@drawable/popup_bg"    android:gravity="center">    <LinearLayout          android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/ll_toast">    <ImageView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:src="@drawable/popup_ico_prompt"        android:layout_marginRight="23dp"/>    <LinearLayout         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:orientation="vertical">         <TextView             android:id="@+id/tv_toast_txt0"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textSize="24sp"        android:textColor="@color/color_ee"/>        <!-- <TextView             android:id="@+id/tv_toast_txt1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="24sp"            android:visibility="gone"            android:textColor="@color/color_ee"/> -->    </LinearLayout>   </LinearLayout></LinearLayout>

通过这个分析,可以解决之前自己提出的一个问题:

http://bbs.csdn.net/topics/390710844?page=1#post-398795270


在此mark一下


结合上文,今天使用自定toast的时候又出现了这个问题。

xml文件中设置了一个textview,想让其宽度自适应文字内容的长度。如下代码所示:

toast_submit2.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical" >             <TextView         android:id="@+id/tv_toast"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/toast_tv_boder"        android:paddingLeft="10dp"        android:paddingRight="10dp"        android:paddingTop="2dp"        android:paddingBottom="2dp"        android:text="adb"        android:textSize="18sp"/>       </LinearLayout>


直接将此xml加载到toast中如下所示:

public static void showToast1(Activity act,CharSequence text, int length) { // TODO Auto-generated method stub if (sToast == null) { LayoutInflater inflater = act.getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_submit2, null); TextView textView = (TextView) layout.findViewById(R.id.tv_toast); textView.setText(text);  sToast = new Toast(act.getApplicationContext()); sToast.setView(layout); sToast.setGravity(Gravity.BOTTOM, 0, 300); } sToast.setDuration(length); sToast.show(); }
实际效果显示,toast里面的textview里面的内容文字太多的时候始终会出现换行(这个时候文字长度并没有达到屏幕的宽度)。按以前的做法是代码里动态设置其layoutParam属性。今天看到一个仁兄的方法,如上面论坛里面的连接,遂始终之,很简单,在setGravity中加入这么一句:Gravity.FILL_HORIZONTAL,即
sToast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 300);

我的理解是,这样使得整个toast横向充满了屏幕,父控件宽度确定了,那么子控件的宽度也就能自适应了。

于是运行,结果发现,toast里面的内容提示位置显示在了最左边,而我需要的是居中显示,这个就好办了,设置xml中的跟布局的gravity属性为居中不就可以了么。于是在toast_submit2xml的根布局LinearLayout下加入属性:android:gravity="center"

到此,显示正确了!





0 0