android - 在 Android 中生成动态视图
问题描述
我想生成如下视图。
这个视图应该是动态生成的。这意味着视图总数可能会不时变化。最多可以有 6 到 7 个视图。我尝试使用线性布局来实现这一点,但是当设备宽度发生变化时,视图会像这样发生变化。
这是我的代码。
List<String> ticketNumbers = Arrays.asList(buyLottery.getTicketNumbers());
LinearLayout[] linearLayoutArr = null;
linearLayoutArr = new LinearLayout[ticketNumbers.size()];
holder.llLotteryViewContainer.removeAllViews();
// Setting dimensions of Views
int width = Values.pxToDp(context, 45);
int height = Values.pxToDp(context, 45);
int textSize = Values.pxToSp(context, 6);
int margin = Values.pxToDp(context, 5);
int imgWidth = Values.pxToDp(context, 45);
int imgHeight = Values.pxToDp(context, 45);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params2.width = ViewGroup.LayoutParams.WRAP_CONTENT;
params2.height = ViewGroup.LayoutParams.WRAP_CONTENT;
params2.gravity = Gravity.CENTER;
params2.weight = 1.0f;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
params.setMargins(margin, margin, margin, margin);
for (int i = 0; i < linearLayoutArr.length; i++) {
linearLayoutArr[i] = new LinearLayout(context);
if (linearLayoutArr[i] != null) {
linearLayoutArr[i].setOrientation(LinearLayout.HORIZONTAL);
linearLayoutArr[i].setLayoutParams(params2);
// <editor-fold default state="collapsed" desc="Digit">
if (Character.isDigit(ticketNumbers.get(i).charAt(0))) {
textView = null;
textView = new TextView(context);
textView.setWidth(width);
textView.setHeight(height);
textView.setText(ticketNumbers.get(i));
textView.setTextSize(textSize);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(context.getResources().getColor(R.color.black));
textView.setLayoutParams(params);
if (selectedLottery.getBonusNumber().equals("YES") && Integer.parseInt(selectedLottery.getBonusNumberPos()) - 1 == i) {
textView.setBackground(ContextCompat.getDrawable(textView.getContext(), R.drawable.background_bonus_bubble));
linearLayoutArr[i].addView(textView);
holder.llLotteryViewContainer.addView(linearLayoutArr[i], 0);
holder.llLotteryViewContainer.invalidate();
} else {
textView.setBackground(context.getResources().getDrawable(R.drawable.ic_background_normal_bubble));
linearLayoutArr[i].addView(textView);
holder.llLotteryViewContainer.addView(linearLayoutArr[i]);
}
}
// </editor-fold>
// <editor-fold default state="collapsed" desc="Letter">
else if (Character.isLetter(ticketNumbers.get(i).charAt(0))) {
if (ticketNumbers.get(i).length() > 1) {
String zodiac = ticketNumbers.get(i);
String zodiacName = Values.getZodiacIdFromName(zodiac);
int imgId = Values.getId(zodiacName, R.drawable.class);
ImageView imageView = new ImageView(context);
imageView.setBackground(context.getResources().getDrawable(imgId));
imageView.setColorFilter(context.getResources().getColor(R.color.colorPrimary));
imageView.setLayoutParams(params);
imageView.getLayoutParams().height = imgHeight;
imageView.getLayoutParams().width = imgWidth;
linearLayoutArr[i].addView(imageView);
holder.llLotteryViewContainer.addView(linearLayoutArr[i], 0);
} else {
textView = null;
textView = new TextView(context);
textView.setWidth(width);
textView.setHeight(height);
textView.setText(ticketNumbers.get(i));
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setTextSize(textSize);
textView.setGravity(Gravity.CENTER);
textView.setTextColor(context.getResources().getColor(R.color.black));
textView.setLayoutParams(params);
linearLayoutArr[i].addView(textView);
textView.setBackground(context.getResources().getDrawable(R.drawable.ic_background_special_bubble));
holder.llLotteryViewContainer.addView(linearLayoutArr[i], 0);
}
holder.llLotteryViewContainer.invalidate();
}
// </editor-fold>
}
}
我想知道构建此布局的更好方法。使用网格布局而不是线性布局更好吗?如果是,我想知道如何。
解决方案
推荐阅读
- java - Java REST 客户端 - 错误请求 (400) - 由于语法错误,服务器无法理解请求
- python-3.x - 计数来自另一列的 Id 的总行数
- asp.net - ASP.Net Core 3.1 - IIS 10.0 - Windows 身份验证
- angular - 如何以角度 4 读取/保存 HTTP Post 响应(HTML 格式)
- sql - 我想在我的 SQL 表中连接多个 ID
- java - Spring Boot 安全 - 基本身份验证
- javascript - 来自 localhost 的 GET 返回代码 404(未找到)
- oracle - Oracle 11g 可能的最大语句长度
- node.js - 全局变量未更新
- android - Firebase 针对修改后的 APK 和 MITM 攻击的安全性