我正在尝试制作如下所示的数据库项目列表.. for (int i=0; i < jArray.length(); i++) { JSONObject row = jArray.getJSONObject(i); LinearLayout ll = new LinearLayout(MainActivity.this); ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,100)); Button btn = new Button(MainActivity.this); //btn.setText(row.getString("subject")); btn.setText(String.valueOf(i)); btn.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.8)); File imgFile = new File(file_path); Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); ImageView iv = new ImageView(MainActivity.this); iv.setScaleType(ImageView.ScaleType.FIT_XY); iv.setImageBitmap(myBitmap); iv.setLayoutParams(new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2)); ll.addView(btn); ll.addView(iv); LinearLayout sv = findViewById(R.id.sv_layout); sv.addView(ll); }结果是这样的:但我想在线性布局之间留出空间并使图像高度更短以与按钮高度相等我怎样才能做到这一点?谢谢,
3 回答
凤凰求蛊
TA贡献1825条经验 获得超4个赞
在布局参数中设置边距并将它们传递给您的 ImageView 对象。
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,(float)0.2);
int dp = getDps(8);
layoutParams.setMargins(0,dp,0,dp);
iv.setLayoutParams(layoutParams);
函数 getDps():
public int getDps(int sizeInDp){
float scale = getResources().getDisplayMetrics().density;
return (int) (sizeInDp*scale + 0.5f);
}
这看起来像:
有只小跳蛙
TA贡献1824条经验 获得超8个赞
请试试这个代码
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100); params.setMargins(0, 5, 5, 10); ll.setLayoutParams(params);
您可以在此行中添加您的特定边距 params.setMargins(0, 5, 5, 10);
添加回答
举报
0/150
提交
取消