android动态创建linear,Android动态添加view的方法示例

由于项目需求菜单写活,效果如下:

15084837021.png?2017920962

这里的按钮数量是可变的.png

由于不是可滑动控件,我用的百分比布局做的适配

LinearLayout typeLayout = (LinearLayout) headerView.findViewById(R.id.layout_type);

final List firstTypeList = entity.getData();

for (int i = 0;i

WindowManager wm = (WindowManager) getContext()

.getSystemService(Context.WINDOW_SERVICE);

int width = wm.getDefaultDisplay().getWidth();

int height = wm.getDefaultDisplay().getHeight();

View view = View.inflate(getActivity(),R.layout.item_first_type,null);

LinearLayout tab = (LinearLayout) view.findViewById(R.id.tab);

LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) tab.getLayoutParams();

linearParams.width = width/firstTypeList.size();//根据数量来吧

linearParams.height = width/firstTypeList.size();//根据数量来吧

tab.setLayoutParams(linearParams); //使设置好的布局参数应用到控件

item_first_type代码:

android:id="@+id/tab"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:gravity="center"

android:background="@drawable/selector_choose_white">

android:id="@+id/iv"

android:layout_width="0dp"

app:layout_widthPercent="45%h"

android:layout_height="0dp"

app:layout_heightPercent="45%h"

android:src="@mipmap/first_newenergy_tab"/>

android:id="@+id/tv"

android:layout_width="match_parent"

android:layout_height="0dp"

app:layout_heightPercent="20%h"

android:text="新能源"

android:textColor="@color/black_my"

app:layout_textSizePercent="12%"

android:gravity="center"

android:maxLines="1"

android:ellipsize="end"/>

layout_type代码:

android:id="@+id/layout_type"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal"

android:background="@color/white">

这种写法数量一般3-6个还是可以的,如果太多的话还是推荐用RecyclerView。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

小编个人微信号 jb51ccc

喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值