繁体   English   中英

以编程方式将ImageView与LinearLayout对齐

[英]Aligning ImageView to LinearLayout programmatically

我需要以编程方式将ImageView对准LinearLayout的底部。

当然,我已经尝试将ImageView作为LinearLayout的子项添加,但是图像会缩小(因为LinearLayout小于图像),并且我不需要调整图像的大小,而只能将其底部对齐。

我也尝试过这个:

    RelativeLayout main_layout = (RelativeLayout) view.findViewById(R.id.main_layout);
    LinearLayout line_0_layout = (LinearLayout) view.findViewById(R.id.line_0_layout);

    ImageView horse_img = new ImageView(getActivity());
    horse_img.setImageResource(R.drawable.horse);

    RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layout_params.addRule(RelativeLayout.ALIGN_BOTTOM, line_0_layout.getId());

    horse_img.setLayoutParams(layout_params);
    main_layout.addView(horse_img);

但它不起作用:图像已添加到视图中,但未与LinearLayout对齐。

似乎很简单的问题,但事实并非如此。

在此先感谢您的帮助。

layout_params.addRule(RelativeLayout.BELOW, line_0_layout.getId());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM