Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式在两行中定位ImageView_Android_Android Layout_Imageview - Fatal编程技术网

Android 以编程方式在两行中定位ImageView

Android 以编程方式在两行中定位ImageView,android,android-layout,imageview,Android,Android Layout,Imageview,我有一个代码,生成几个ImageView并将其放在布局上 for (int i = 0; i < NUMBER_OF_MATCHES; i++) { imageView = new ImageView(this); if (random.nextBoolean()) { imageView.setImageResource(R.drawable.match); } else { imageView.se

我有一个代码,生成几个ImageView并将其放在布局上

for (int i = 0; i < NUMBER_OF_MATCHES; i++) {
        imageView = new ImageView(this);
        if (random.nextBoolean()) {
        imageView.setImageResource(R.drawable.match);
        } else {
            imageView.setImageResource(R.drawable.match_inverse);

        }
        gameLinearLayout.addView(imageView, 0, params);
    }
for(int i=0;i

但所有图像都在一行中。我想把它分成两行。使用哪种布局以及如何修复代码以正确工作?

如果我理解正确,您需要两行独立的图像。 因此,我们需要一个垂直方向的基线布局来容纳每一行,而每一行由一个水平方向的基线布局组成:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

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

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

</LinearLayout> 

请查看此处,了解发生这种情况的原因:

请看这里对本帖最后一个答案的解释,该答案涉及相对性规则:

按如下方式进行尝试:

        //LinearLayOut Setup
        LinearLayout linearLayout= new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        linearLayout.setLayoutParams(new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
    for (int i = 0; i < NUMBER_OF_MATCHES; i++) 
     {
        //ImageView Setup
        ImageView imageView = new ImageView(this);
        //setting image resource
  if (random.nextBoolean()) {
    imageView.setImageResource(R.drawable.match);
    } else {
        imageView.setImageResource(R.drawable.match_inverse);

    }

        //setting image position
        imageView.setLayoutParams(linearLayout);

        //adding view to layout
        linearLayout.addView(imageView);
 }
//线性布局设置
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setOrientation(linearLayout.VERTICAL);
linearLayout.setLayoutParams(新的LayoutParams(
LayoutParams.MATCH_父级,
LayoutParams.MATCH_PARENT));
for(int i=0;i<匹配的数量;i++)
{
//ImageView设置
ImageView ImageView=新的ImageView(此);
//设置图像资源
if(random.nextBoolean()){
setImageResource(R.drawable.match);
}否则{
setImageResource(R.drawable.match_reverse);
}
//设置图像位置
设置布局参数(linearLayout);
//向布局添加视图
linearLayout.addView(图像视图);
}

尝试使用
LinearLayout
。从命名来看,他使用的是LinearLayout,而不是RelativeLayout。哦,看来你是对的,我们需要xml代码来了解他是如何使用它的。。