由于项目需求菜单写活,效果如下:
这里的按钮数量是可变的.png
由于不是可滑动控件,我用的百分比布局做的适配
LinearLayout typeLayout = (LinearLayout) headerView.findViewById(R.id.layout_type);
final List firstTypeList = entity.getData();
for (int i = 0;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代码:
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">
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"/>
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"/>
layout_type代码:
android:id="@+id/layout_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@color/white">
这种写法数量一般3-6个还是可以的,如果太多的话还是推荐用RecyclerView。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
小编个人微信号 jb51ccc
喜欢与人分享编程技术与工作经验,欢迎加入编程之家官方交流群!