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

首頁 > 系統 > Android > 正文

Android動態添加view的方法示例

2019-10-22 18:25:25
字體:
來源:轉載
供稿:網友

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

Android動態添加view,動態添加webview

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

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 满城县| 云浮市| 靖边县| 东乌珠穆沁旗| 深圳市| 兴国县| 黄龙县| 通化市| 浮山县| 吉木乃县| 仁怀市| 和静县| 安阳县| 闽清县| 景谷| 玉屏| 皮山县| 六盘水市| 慈利县| 武山县| 通化市| 繁昌县| 昌黎县| 鹤庆县| 安国市| 东海县| 秦皇岛市| 牡丹江市| 都江堰市| 榆林市| 微博| 上杭县| 正蓝旗| 玛多县| 苍山县| 博湖县| 金川县| 祁东县| 拉萨市| 南昌市| 威海市|