Android動態(tài)添加view的方法示例
更新時間:2017年10月20日 09:04:47 作者:楊帥
本篇文章主要介紹了Android動態(tài)添加view的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
由于項目需求菜單寫活,效果如下:
這里的按鈕數(shù)量是可變的.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();//根據(jù)數(shù)量來吧 linearParams.height = width/firstTypeList.size();//根據(jù)數(shù)量來吧 tab.setLayoutParams(linearParams); //使設(shè)置好的布局參數(shù)應(yīng)用到控件 }
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>
這種寫法數(shù)量一般3-6個還是可以的,如果太多的話還是推薦用RecyclerView。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android使用addView動態(tài)添加組件的方法
- android ListView內(nèi)數(shù)據(jù)的動態(tài)添加與刪除實例代碼
- Android動態(tài)添加menu菜單的簡單方法
- 在Android中動態(tài)添加Panel框架的實現(xiàn)代碼
- Android 動態(tài)添加Fragment的實例代碼
- Android用RecyclerView實現(xiàn)動態(tài)添加本地圖片
- Android編程實現(xiàn)長按Button按鈕連續(xù)響應(yīng)功能示例
- Android實現(xiàn)圓角Button按鈕
- Android開發(fā)中button按鈕的使用及動態(tài)添加組件方法示例
相關(guān)文章
基于Android實現(xiàn)點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出
這篇文章主要介紹了基于Android實現(xiàn)點擊某個按鈕讓菜單選項從按鈕周圍指定位置彈出的相關(guān)資料,需要的朋友可以參考下2015-12-12Android開發(fā)中使用Volley庫發(fā)送HTTP請求的實例教程
這篇文章主要介紹了Android開發(fā)中使用Volley庫發(fā)送HTTP請求的實例教程,包括創(chuàng)建Volley單例的基本知識與取消Request請求的技巧等,需要的朋友可以參考下2016-05-05android開發(fā)實現(xiàn)列表控件滾動位置精確保存和恢復(fù)的方法(推薦)
下面小編就為大家?guī)硪黄猘ndroid開發(fā)實現(xiàn)列表控件滾動位置精確保存和恢復(fù)的方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03