以编程方式设置的ImageViews不会出现

时间:2013-09-26 10:32:54

标签: android arrays dimensional

我有一个2维。 ImageViews数组。它们以编程方式设置为RelativeLayout。但是当我启动应用程序时,它们根本就没有出现。 这是我的代码:

private void setMap(ImageView[][] fields){
    RelativeLayout relLay = (RelativeLayout) findViewById(R.id.screen);
    OnClickListener listener = new MyOnClickListener();
    for (int i = 0; i < numFieldsHeight; i++){
        for (int j = 0; j < numFieldsWidth; j++){
            ImageView img = new ImageView(MapActivity.this);
            img.setImageResource(R.drawable.map);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            if (j != 0)
                params.addRule(RelativeLayout.RIGHT_OF, fields[i][j-1].getId());
            else
                params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
            if (i != 0)
                params.addRule(RelativeLayout.BELOW, fields[i-1][j].getId());
            else
                params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            params.height = fieldLength;
            params.width = fieldLength;
            img.setVisibility(View.VISIBLE);
            img.setId(getFieldId(j,i));
            img.setOnClickListener(listener);
            fields[i][j] = img;
            relLay.addView(fields[i][j], params);
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000" >

    <RelativeLayout 
        android:id="@+id/relLayDiffSeekBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        ...

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/screen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="#222222" >

    </RelativeLayout>

</RelativeLayout>

RelativeLayout“screen”的其他属性在代码中设置。 但这有效,我可以看到RelativeLayout,只是缺少ImageViews。

没有抛出异常。请帮帮我!

------!编辑!------

我很抱歉,这段代码实际上正在运行。问题是我忘了将变量fieldLength设置为不同于0的值。 非常感谢你的帮助! :)

2 个答案:

答案 0 :(得分:0)

Drawable d = getResources().getDrawable(R.drawable.map);
d.setBounds(0, 0, 50, 50);
img.setImageDrawable(d);

答案 1 :(得分:0)

public void horizontalScrollGalleryLayout () {
    sv=new HorizontalScrollView(this);
    LinearLayout llh = new LinearLayout(this);
    llh.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams layoutParamsTV = new LinearLayout.LayoutParams(90,90);
    LinearLayout.LayoutParams layoutParamsLL = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams layoutParamsLLD = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParamsTV.setMargins(10, 5, 5, 5);
    for(int k=0;k<6;k++)
    {   
        LinearLayout llv = new LinearLayout(this);
        for (int i=0; i<4; i++) {
            llv.setOrientation(LinearLayout.VERTICAL);
            img=new ImageButton(this);
            try {
                img.setBackgroundResource(integerIDs[total]);
            } catch (Exception e) {

                e.printStackTrace();
            }
            llv.addView(img, layoutParamsTV);
        }
        llh.addView(llv, layoutParamsLL);
        llh.setBackgroundColor(Color.TRANSPARENT);
    }
    sv.setBackgroundColor(Color.GRAY);
    sv.addView(llh, layoutParamsLLD);
    setContentView(sv);

}

这可能是解决方案之一......用于创建Matrix网格 为你问题。试试吧......任何问题让我知道