记录一下最近在项目的滚动视图里动态添加按钮的实现方法
一 xml的布局
1.主界面contentView的布局
<LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F3F3F3"
android:orientation="vertical">
<ScrillView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//...其它模块
<LinearLayout
android:id="@+id/id_hp_dynamic_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white_fc"
>
</LinearLayout>
//...其它模块
</LinearLayout>
</ScrillView>
</LinearLayout>
2.新增按钮button的布局
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"
android:id="@+id/id_hp_itembtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:textSize="12dp"
>
</Button>
二 java文件里实现
1.声明文字数组、图片数组
List<String> temlist = new ArrayList();
List<Integer>funtionModImg = new ArrayList<>;
private LinearLayout contentView
2.获取数据
(注:因为我的数据时动态获取的,这里使用了SharedPreferences来保存List<T>数据,使用方法可以参考Android SharedPreferences保存List<T>数据文章)
SharedPreferences sp = getActivity().getSharedPreferences("xxx",Context.MODE_PRIVATE);
String str = sp.getString("serial","");
if (str != ""){
Gson gson = new Gson();
temlist = gson.fromJson(str,new TypeToken<List< String>>(){}.getType());
}
...
//注:项目里的temlist={"DAS","DAS HD","SPECIAL","ME","OBD II","IMMO"};
//temlist对应的图片数组funtionModImg={R.mipmap.hp_obd_icon,R.mipmap.hp_das_icon,R.mipmap.hp_dashd_icon,R.mipmap.hp_special_icon,R.mipmap.hp_me_icon,R.mipmap.hp_obd_icon,R.mipmap.hp_immo_icon};
3.加载布局
contentView = view.findViewById(R.id.id_hp_dynamic_btn);
LinearLayout parentLayout = (LinearLayout)contentView;
int size = temlist.size();
//每行的水平LinearLayout
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0,0,0,0);
ArrayList<Button> childBtns = new ArrayList<>();
//记录加载的按钮个数
int totoalBtn = 0;
//WindowManager方法获取屏幕的宽度
WindowManager wm = (WindowManager) this.getActivity().getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
//动态创建按钮
for (int i = 0;i < size ;i++){
String item = temlist[I];
String item1 = funtionModImg.get(i);
Integer item2 = functionModuleTag.get(i);
LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
Button childBtn = (Button)LayoutInflater.from(this.getActivity()).inflate(R.layout.activity_hp_fuction_module,null);
childBtn.setText(item);
totoalBtn++;
//设置每个按钮的宽
itemParams.width = width/4;
childBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position =(Integer)v.getTag();
//在这里处理对应按钮事件
Log.e("TAG", "onClick: position"+position );
}
});
//添加tag值
childBtn.setTag(i);
childBtn.setBackgroundColor(255);
childBtn.setLayoutParams(itemParams);
//设置按钮图标
childBtn.setCompoundDrawablesRelativeWithIntrinsicBounds(0,item1,0,0);
childBtns.add(childBtn);
//大于3个的时候换行
if (totoalBtn > 3){
LinearLayout horizLL = new LinearLayout(this.getActivity());
horizLL.setOrientation(LinearLayout.HORIZONTAL);
horizLL.setLayoutParams(layoutParams);
for (Button addBtn:childBtns){
horizLL.addView(addBtn);
}
parentLayout.addView(horizLL);
childBtns.clear();
totoalBtn = 0;
}
}
//最后一行添加一下
if (!childBtns.isEmpty()){
LinearLayout horizLL = new LinearLayout(this.getActivity());
horizLL.setOrientation(LinearLayout.HORIZONTAL);
horizLL.setLayoutParams(layoutParams);
for (Button addBtn:childBtns){
horizLL.addView(addBtn);
}
parentLayout.addView(horizLL);
childBtns.clear();
totoalBtn = 0;
}
三 效果图
网友评论