国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android動態添加view的方法示例

2019-12-12 01:51:52
字體:
來源:轉載
供稿:網友

由于項目需求菜單寫活,效果如下:

這里的按鈕數量是可變的.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。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇雄县| 乌兰察布市| 云霄县| 马山县| 祥云县| 阿瓦提县| 巴马| 哈密市| 宁晋县| 巴彦县| 宁都县| 西林县| 南安市| 涟水县| 高邑县| 绥德县| 德兴市| 海淀区| 游戏| 永济市| 常宁市| 辽阳市| 庄浪县| 章丘市| 吉安市| 嘉禾县| 疏附县| 西乌珠穆沁旗| 怀来县| 白河县| 平塘县| 安仁县| 抚顺县| 南开区| 合作市| 佛教| 米泉市| 三原县| 宝坻区| 峨边| 福海县|