我创建了上面的弹出窗口,其内容由主垂直LinearLayout中的水平LinearLayout视图行组成。每个水平LinearLayout包含一个ImageView和一个TextView。 我在PopupWindow中创建它,并以编程方式执行此操作,以便我可以根据需要更改ImageView源。
正如您所看到的那样,第一个图标似乎占用了大量空间,尽管与其他图标生成的代码相同。
以下是代码:
LinearLayout verticalLayout = new LinearLayout(context);
verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams mainLayoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
verticalLayout.setLayoutParams(mainLayoutParams);
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams iconParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams textParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//History row
LinearLayout historyLayout = new LinearLayout(context);
historyLayout.setLayoutParams(layoutParams);
historyLayout.setOrientation(LinearLayout.HORIZONTAL);
ImageView historyIcon = new ImageView(context);
historyIcon.setImageResource(R.drawable.small_book_grey);
historyIcon.setAdjustViewBounds(true);
historyIcon.setLayoutParams(iconParams);
historyLayout.addView(historyIcon);
TextView historyText = new TextView(context);
historyText.setLayoutParams(textParams);
historyText.setText("History");
historyLayout.addView(historyText);
verticalLayout.addView(historyLayout);
//Exam row...
//... (duplicate of history row)
我尝试过使用布局参数,甚至创建一个模拟xml布局,显示我喜欢的内容,以匹配参数。 如果有人能给出一些关于使那本书的图标与其他图书图标大小相同的建议,我将不胜感激。
答案 0 :(得分:0)
将scaleType添加到fitCenter的ImageView
答案 1 :(得分:0)
将此代码写在historyIcon.setAdjustViewBounds(true);
historyIcon.setWidth()`
根据你的布局放宽度。
答案 2 :(得分:0)
虽然我没有弄清楚为什么第一张图像与其他图像的缩放比例不同,但我确实找到了另一种解决方案:使用复合左图。
historyText.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Drawable img = m_context.getResources().getDrawable(
R.drawable.small_book_grey);
img.setBounds(0, 0, img.getIntrinsicWidth() * historyText.getMeasuredHeight() / img.getIntrinsicHeight(), historyText.getMeasuredHeight());
historyText.setCompoundDrawables(img, null, null, null);
historyText.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
手动设置与TextView匹配的边界。看起来很笨重,但这是我能够做到我的目标的唯一方法。