繁体   English   中英

在 LinearLayout 中以编程方式添加 ImageView

[英]Programmatically Adding ImageViews in LinearLayout

我正在以编程方式创建简单的LinearLayout ,它有两个子( Imageviews )占据相等的宽度,在这种情况下,Rootview 是SquareLinearLayoutSquareLinearLayout )。

这是 SquareLinearLayout

public class SquareLinearLayout extends LinearLayout {

    public SquareLinearLayout(Context context) {
        super(context);
    }

    public SquareLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int orientation = getContext().getResources().getConfiguration().orientation;

        if (orientation == Configuration.ORIENTATION_PORTRAIT || orientation == Configuration.ORIENTATION_UNDEFINED) {

            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            setMeasuredDimension(width, height);

        } else {

            int height = MeasureSpec.getSize(heightMeasureSpec);
            int width = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            setMeasuredDimension(width, height);

        }

    }
}

相关 XML

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <com.customviews.SquareLinearLayout
        android:id="@+id/lay_square"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ececec"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/lay_r_capture"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
        </LinearLayout>

    </com.customviews.SquareLinearLayout>

</LinearLayout>

以编程方式在 LinearLayout 中添加 ImageView

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture);
LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
lay_r_capture.setOrientation(LinearLayout.HORIZONTAL);
lay_r_capture.setWeightSum(2f);
lay_r_capture.setLayoutParams(layparam);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1f);
    lp.setMargins(8,8,8,8);

ImageView img1 = new ImageView(this);
Bitmap photo1 = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.photo_1);
img1.setLayoutParams(lp);
img1.setImageBitmap(photo1);

ImageView img2 = new ImageView(this);
Bitmap photo2 = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.photo_2);
img2.setLayoutParams(lp);
img2.setImageBitmap(photo2);

lay_r_capture.addView(img1);
lay_r_capture.addView(img2);

这东西不像预期的那样工作这个

输出 :

在此处输入图片说明

我缺少哪些属性或LayoutParams

//you can set imageView like  //img1.setImageResource(R.drawable.photo_2);    

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture);
    LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    lay_r_capture.setOrientation(LinearLayout.HORIZONTAL);
    lay_r_capture.setWeightSum(2f);
    lay_r_capture.setLayoutParams(layparam);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1f);
        lp.setMargins(8,8,8,8);

    ImageView img1 = new ImageView(this);
    img1.setImageResource(R.drawable.photo_1);
    img1.setLayoutParams(lp);

    ImageView img2 = new ImageView(this);
    img2.setImageResource(R.drawable.photo_2);
    img2.setLayoutParams(lp);

    lay_r_capture.addView(img1);
    lay_r_capture.addView(img2);

您面临的问题是因为SquareLinearLayout SquareLinearLayout onMeasure()中,您正在更改视图的宽度和高度以实现方形布局。

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int orientation = getContext().getResources().getConfiguration().orientation;

        //The below code will set width = height, to achieve square layout
        if (orientation == Configuration.ORIENTATION_PORTRAIT || orientation == Configuration.ORIENTATION_UNDEFINED) {

            int width = MeasureSpec.getSize(widthMeasureSpec);
            int height = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
            setMeasuredDimension(width, height);

        } else {

            int height = MeasureSpec.getSize(heightMeasureSpec);
            int width = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
            setMeasuredDimension(width, height);

        }

您在此处以编程方式将MATCH_PARENT设置为高度:

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture);
LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
lay_r_capture.setOrientation(LinearLayout.HORIZONTAL);
lay_r_capture.setWeightSum(2f);
lay_r_capture.setLayoutParams(layparam);

因此,将其更改为WRAP_CONTENT例如:

lay_r_capture = (LinearLayout)findViewById(R.id.lay_r_capture);
LinearLayout.LayoutParams layparam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lay_r_capture.setOrientation(LinearLayout.HORIZONTAL);
lay_r_capture.setWeightSum(2f);
lay_r_capture.setLayoutParams(layparam);

暂无
暂无

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

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