代码写UI实现一页6个6宫格
private void addLongViewHomePagerView(LinearLayout mLinearLayoutHome) {
LinearLayout.LayoutParams mViewPagerParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
mViewPagerParams.weight = weight_home_pagerview;
int dot_bottomMargin = ConfigLauncherUI.getInstance().getDotUIData()
.getBottomMargin();
mLongViewPagerChild = new LauncherMainSixGridChild(mContext);
ArrayList<View> mViewList = mLongViewPagerChild.getmViewList();
LongViewPager mLongViewPager = new LongViewPager(mContext, mViewList,
false, dot_bottomMargin,
LauncherMainSixGrid.class.getSimpleName());
mLongViewPager.setLayoutParams(mViewPagerParams);
mLinearLayoutHome.addView(mLongViewPager);
}
每页的实现
package com.spt.carengine.view;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ImageView.ScaleType;
import com.spt.carengine.utils.LogUtil;
import com.spt.carengine.view.utils.ConfigLauncherUI;
import com.spt.carengine.view.utils.ConfigThemeImageDrawable;
import com.spt.carengine.view.utils.ViewOnclickListener;
import com.spt.carengine.view.utils.ConfigLauncherUI.LauncherUIDataParam;
import java.util.ArrayList;
public class LauncherMainSixGridChild {
private static final String TAG = LauncherMainSixGridChild.class.getSimpleName();
private int layout_padding = 4;// layout内边距
private int btn_spacing = 4;// 图片与图片之间的间距
private boolean isOnlyOnePicToClick = true;
private String[] imgSrcData = null;
private int[] weights = {
1, 1, 1, 3
};
private int[][] mHomeBtnLayoutParams = null;
private LauncherUIDataParam mLauncherUIDataParam = null;
private String txtData[] = null;
private void initializedData() {
btn_spacing = 1;
layout_padding = 4;
weights = new int[] {
234, 262, 234, 730
};
mHomeBtnLayoutParams = new int[][] {
{
234, 240
}, {
262, 240
}, {
234, 240
}, {
234, 239
}, {
262, 239
}, {
234, 239
}
};
mLauncherUIDataParam = ConfigLauncherUI.getInstance().getHomeUIData();
imgSrcData = mLauncherUIDataParam.getBtn();
txtData = mLauncherUIDataParam.getText();
LogUtil.i(TAG,
"imgSrcData = " + (imgSrcData == null ? 0 : imgSrcData.length));
isOnlyOnePicToClick = true;
}
private static final int PAGE_DEFAULT_NUMBER = 6;// 设置每个页面默认显示卡片个数
private static final int ROW_NUMBER = 2;// 两行
private static final int COLUMN_NUMBER = 3;// 三列
private Context context;
private ArrayList<View> mViewList = new ArrayList<View>();
public LauncherMainSixGridChild(Context context) {
this.context = context;
initializedData();
initView();
}
public ArrayList<View> getmViewList() {
return mViewList;
}
public void setmViewList(ArrayList<View> mViewList) {
this.mViewList = mViewList;
}
public void initView() {
LinearLayout linearLayout = null;
LinearLayout linearLayoutChild = null;
ImageView imageView = null;
if (imgSrcData != null) {
RelativeLayout.LayoutParams mTxtParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mTxtParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout.LayoutParams mReLayoutParams = new RelativeLayout.LayoutParams(
mLauncherUIDataParam.getWidth(), mLauncherUIDataParam.getHeight());
mReLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
int leng = imgSrcData.length % PAGE_DEFAULT_NUMBER == 0
? (imgSrcData.length / PAGE_DEFAULT_NUMBER)
: (imgSrcData.length / PAGE_DEFAULT_NUMBER + 1);
for (int i = 0; i < leng; i++) {
linearLayout = new LinearLayout(context);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setWeightSum(2);
linearLayout.setPadding(layout_padding, 0, layout_padding, 0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
for (int j = 0; j < ROW_NUMBER; j++) {
linearLayoutChild = new LinearLayout(context);
linearLayoutChild.setOrientation(LinearLayout.HORIZONTAL);
linearLayoutChild.setWeightSum(weights[weights.length - 1]);
LinearLayout.LayoutParams layoutParamsChild = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
layoutParamsChild.weight = 1;
linearLayoutChild.setLayoutParams(layoutParamsChild);
linearLayout.addView(linearLayoutChild);
if (j < ROW_NUMBER - 1) {
View view = new View(context);
LinearLayout.LayoutParams layoutParamsV = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
btn_spacing);
view.setLayoutParams(layoutParamsV);
linearLayout.addView(view);
}
for (int k = 0; k < COLUMN_NUMBER; k++) {
int index = i * PAGE_DEFAULT_NUMBER + j * COLUMN_NUMBER
+ k;
int arrayIndex = index % (ROW_NUMBER * COLUMN_NUMBER);
int[] params = mHomeBtnLayoutParams[arrayIndex];
if (index < imgSrcData.length) {
LinearLayout homeLayout = new LinearLayout(
context);
LinearLayout.LayoutParams homeLayoutParams = new LinearLayout.LayoutParams(
params[0], params[1]);
homeLayout.setGravity(Gravity.CENTER);
homeLayout.setOrientation(LinearLayout.VERTICAL);
homeLayout
.setLayoutParams(homeLayoutParams);
homeLayout
.setBackgroundResource(ConfigThemeImageDrawable
.getDrawableResId(context,
ConfigLauncherUI
.getInstance()
.getHomeUIData()
.getBg()));
String imgTag = imgSrcData[index];
LogUtil.i(TAG, "imgTag = " + imgTag);
imageView = ConfigThemeImageDrawable.getImageView(
context, imgTag, isOnlyOnePicToClick);
imageView.setScaleType(ScaleType.CENTER_INSIDE);
imageView.setTag(imgTag);
imageView.setOnClickListener(ViewOnclickListener
.getInstance());
imageView.setLayoutParams(mReLayoutParams);
imageView.setId(10);
homeLayout.addView(imageView);
TextViewSysFontColor mtxtViewSysFontColor = new TextViewSysFontColor(
context);
mtxtViewSysFontColor.setGravity(Gravity.CENTER_HORIZONTAL);
mtxtViewSysFontColor.setTextSize(22);
mtxtViewSysFontColor.setText(txtData[index]);
mtxtViewSysFontColor.setPadding(0, 20, 0, 0);
mtxtViewSysFontColor.setTextColor(Color.rgb(0xff,
0xff, 0xf9));
mtxtViewSysFontColor.setLayoutParams(mTxtParams);
homeLayout.addView(mtxtViewSysFontColor);
linearLayoutChild.addView(homeLayout);
} else {
View v = new View(context);
if (ConfigLauncherUI.CustomerName.AJ
.equals(ConfigLauncherUI.getInstance()
.getCustomerName())) {
v.setBackgroundResource(ConfigThemeImageDrawable
.getDrawableResId(context,
ConfigLauncherUI.getInstance()
.getHomeUIData()
.getBg()));
}
LinearLayout.LayoutParams layoutParamsImg = new LinearLayout.LayoutParams(
params[0], params[1]);
v.setLayoutParams(layoutParamsImg);
linearLayoutChild.addView(v);
}
if (k < COLUMN_NUMBER - 1) {
View view = new View(context);
LinearLayout.LayoutParams layoutParamsV = new LinearLayout.LayoutParams(
btn_spacing,
LinearLayout.LayoutParams.MATCH_PARENT);
view.setLayoutParams(layoutParamsV);
linearLayoutChild.addView(view);
}
}
}
mViewList.add(linearLayout);
}
}
}
public void onDestory() {
// TODO
}
}