动态设置Layout的宽高值
来源:互联网 发布:p2p理财返利平台源码 编辑:程序博客网 时间:2024/05/29 18:37
项目中使用的布局文件test_layout.xml如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/popup_window" android:gravity="left" android:orientation="vertical"> <LinearLayout android:id="@+id/llTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:orientation="horizontal"> <ImageView android:id="@+id/ivTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@android:drawable/ic_input_add" /> <TextView android:id="@+id/tvTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="展开" android:textColor="@color/black" android:textSize="14sp" /> </LinearLayout> <View android:id="@+id/devider1" android:layout_width="fill_parent" android:layout_height="1px" android:layout_below="@id/llTest" android:visibility="gone" android:background="@color/gray" /> <LinearLayout android:id="@+id/llExpand" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/devider1" android:layout_marginTop="-4dp" android:orientation="vertical" android:visibility="gone"> <LinearLayout android:id="@+id/llExpandItem1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/devider1"> <ImageView android:id="@+id/ivExpandItem1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@android:drawable/presence_online" android:visibility="invisible"/> <TextView android:id="@+id/tvExpandItem1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:text="扩展条目1" android:textColor="@color/black" android:textSize="14sp" /> </LinearLayout> <View android:id="@+id/devider2" android:layout_width="fill_parent" android:layout_height="1px" android:layout_below="@id/llExpandItem1" android:visibility="gone" android:background="@color/gray" /> <LinearLayout android:id="@+id/llExpandItem2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@+id/devider1"> <ImageView android:id="@+id/ivExpandItem2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:src="@android:drawable/presence_online" android:visibility="invisible"/> <TextView android:id="@+id/tvExpandItem2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="10dp" android:text="扩展条目2" android:textColor="@color/black" android:textSize="14sp" /> </LinearLayout> </LinearLayout></RelativeLayout>
为了保证展开的Layout布局的宽度与父布局一致,可以采用以下方式实现:
//获取布局中的各个元素View contentView = View.inflate(this, R.layout.test_layout, null);View mllTest = contentView.findViewById(R.id.llTest);View mllExpand = contentView.findViewById(R.id.llExpand);View mTvllExpandItem1 = contentView.findViewById(R.id.llExpandItem1);View mTvllExpandItem2 = contentView.findViewById(R.id.llExpandItem2);PopupWindow mPopupWindow = new PopupWindow(contentView);mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);mPopupWindow.setOutsideTouchable(true);mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
然后采用如下方式获取PopupWindow的宽度值,并利用LayoutParam进行设置。
//由于mllExpand的parent layout是RelativeLayout,因此需要采用RelativeLayout.LayoutParams类型RelativeLayout.LayoutParams lpExpand = (RelativeLayout.LayoutParams)mllExpand .getLayoutParams();//获取popup window的宽度需要先获取content view,然后再获取宽度lpExpand.width = mPopupWindow.getContentView().getMeasuredWidth();lpExpand.setLayoutParams(lpResChoose);
采用这种方式能够动态设置布局或者控件的宽高值。
0 0
- 动态设置Layout的宽高值
- Android 动态设置Layout的显示与否
- 动态设置layout高度
- 动态设置Relative Layout中按钮的layout_align_parent_right属性
- 动态layout的demo
- layout的均分设置
- 如何动态的设置Relative Layout中按钮的layout_align_parent_right属性
- 【AndroidUI控件】如何动态的设置Relative Layout中按钮的layout_align_parent_right属性
- QT里面的layout设置
- 设置layout
- qt 动态布局的例子,dynamic layout
- 动态改变Layout中控件的参数
- 动画进行layout的动态变化
- Android 动态改变Layout的大小
- Android 动态改变Layout的大小
- 关于Layout Constraint的动态update方式
- ZendFramework2学习笔记 静态配置layout模板和动态设置layout模板
- 欢迎界面的 layout/FrameLayout设置
- 汇编语言将回车键的问题和集成开发的问题
- linux命令集锦之curl
- PAT乙级 打印沙漏(20)
- spring与Hibernate的整合(以及spring的声明式事务处理)
- 任意多边形面积
- 动态设置Layout的宽高值
- ansible高级用法
- 模块化程序设计函数与方法
- 一个类似CColorDialog中颜色选择的控件实现
- 10024---CSS Align(对齐)
- 浅析人脸检测之Haar分类器方法
- ASP.NET MVC中几个运用技巧
- 是readdir,还是readdir_r
- UITableViewCell的性能优化