以编程方式在LinearLayout中添加ImageView

时间:2017-01-16 04:44:50

标签: android imageview android-linearlayout android-imageview android-custom-view

我是以编程方式创建简单LinearLayout,其中有两个Child(Imageviews)占用相等的宽度,在这种情况下,Rootview是CustomLayout(SquareLinearLayout)。

这是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);

此内容无效,如 this

输出:

enter image description here

我缺少哪些属性或LayoutParams

3 个答案:

答案 0 :(得分:1)

//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);

答案 1 :(得分:0)

您遇到的问题是SquareLinearLayout。在onMeasure()的{​​{1}}中,您要更改视图的宽度和高度以实现方形布局。

SquareLinearLayout

答案 2 :(得分:0)

您以编程方式将select count(*) from your_table_here;设置为高度:

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