自定义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>
所以以后要改变这个自定义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>
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
- 自定义toast时,有些时候不能设置想要的宽和高(代码动态设置或使用文末方式)
- 重新梳理一下自定义Toast的用法,设置想要的位置,自定义宽和高,自定义显示时间等
- java代码设置布局的宽和高
- wpf 如何动态的设置Grid宽和高
- 根据手机屏幕的大小动态设置图片宽和高
- Android中在代码中设置控件的宽和高
- 代码中动态设置view或布局的宽高
- 处理图片时 想要将图片缩小或放大时候的设置
- 得到屏幕的宽和高,然后在代码里面设置控件的宽和高,加载gif图片
- 使用代码动态设置或修改drawableLeft
- Android中如何在代码中设置View的宽和高
- 解决自定义Toast(吐司)设置宽高无效的问题
- Android使用Java代码设置selector或drawable,以及使用自定义控件方式使用它
- Activity设置全屏以及Android获得屏幕的宽和高
- Activity设置全屏以及Android获得屏幕的宽和高
- Activity设置全屏以及Android获得屏幕的宽和高
- Activity设置全屏以及Android获得屏幕的宽和高
- Activity设置全屏以及Android获得屏幕的宽和高
- SQL问答题
- NSDirectoryEnumerator
- VC 在对话框中增加菜单栏
- hive0.13安装配置元数据库mysql
- 实例讲解 SQL 注入攻击
- 自定义toast时,有些时候不能设置想要的宽和高(代码动态设置或使用文末方式)
- nginx-rtmp-module和simple-rtmp-server搭建
- hibernate SQL hql
- 妈妈圈论坛的设计与实现一maven管理
- VC 在对话框中增加菜单栏
- 9 --> java 数组的定义及使用
- 解释两个类似的js方法为什么运行结果大相径庭
- Saving James Bond - Hard Version
- hadoop2.6、hbase0.96、hive1.13环境搭建过程错误总结