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