Android动态添加view的方法示例

 更新时间:2017年10月20日 09:04:47   作者:楊帥  
本篇文章主要介绍了Android动态添加view的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

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

 LinearLayout typeLayout = (LinearLayout) headerView.findViewById(R.id.layout_type);
 final List<FirstTypeEntity.DataBean> firstTypeList = entity.getData();
 for (int i = 0;i<firstTypeList.size();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代码:

<com.zhy.android.percent.support.PercentLinearLayout
    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">
    <ImageView
      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"/>
    <TextView
      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"/>
  </com.zhy.android.percent.support.PercentLinearLayout>

layout_type代码:

<com.zhy.android.percent.support.PercentLinearLayout
    android:id="@+id/layout_type"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:background="@color/white">
</com.zhy.android.percent.support.PercentLinearLayout>

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

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

相关文章

  • android getActivity.findViewById获取ListView 返回NULL的方法

    android getActivity.findViewById获取ListView 返回NULL的方法

    下面小编就为大家带来一篇android getActivity.findViewById获取ListView 返回NULL的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-11-11
  • android利用handler实现倒计时功能

    android利用handler实现倒计时功能

    这篇文章主要为大家详细介绍了android利用handler实现倒计时功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • Android源码解析之属性动画详解

    Android源码解析之属性动画详解

    在手机上去实现动画效果算是件比较炫酷的事情,自Android 3.0版本开始,系统给我们提供了一种全新的动画模式,属性动画(property animation),它的功能非常强大,弥补了之前补间动画的一些缺陷,几乎是可以完全替代掉补间动画了。本文就详细介绍了Android中的属性动画。
    2017-02-02
  • RxJava 1升级到RxJava 2过程中踩过的一些“坑”

    RxJava 1升级到RxJava 2过程中踩过的一些“坑”

    RxJava2相比RxJava1,它的改动还是很大的,那么下面这篇文章主要给大家总结了在RxJava 1升级到RxJava 2过程中踩过的一些“坑”,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下来要一起看看吧。
    2017-05-05
  • RecyclerView仿应用列表实现网格布局

    RecyclerView仿应用列表实现网格布局

    这篇文章主要为大家详细介绍了RecyclerView仿应用列表实现网格布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-09-09
  • Android基础之隐藏标题栏/设置为全屏/横竖屏切换

    Android基础之隐藏标题栏/设置为全屏/横竖屏切换

    大家好,本篇文章主要讲的是Android基础之隐藏标题栏/设置为全屏/横竖屏切换,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • Android 高仿微信支付数字键盘功能

    Android 高仿微信支付数字键盘功能

    现在很多app的支付、输入密码功能,都是使用自定义数字键盘,方便实用。下面本文给大家带来了Android 高仿微信支付数字键盘功能,非常不错,感兴趣的朋友一起学习吧
    2016-08-08
  • 利用adt-bundle轻松搭建Android开发环境与Hello world(Linux)

    利用adt-bundle轻松搭建Android开发环境与Hello world(Linux)

    这篇文章主要介绍了利用adt-bundle在Linux下轻松搭建Android开发环境与Hello world,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • Andriod使用多线程实现轮播图片

    Andriod使用多线程实现轮播图片

    这篇文章主要介绍了Andriod使用多线程实现轮播图片效果,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-10-10
  • android网络图片查看器简单实现代码

    android网络图片查看器简单实现代码

    这篇文章主要为大家详细介绍了android网络图片查看器的简单实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03

最新评论