线性布局重叠

时间:2014-03-04 11:39:07

标签: android android-linearlayout

出于某种原因,我添加到ScrollView中的RelativeLayout的LinearLayouts彼此重叠。 我假设有一些设置LinearLayouts的方法,所以他们将自己定位在上面的LinearLayout下面?

 exampleLayout = (RelativeLayout)findViewById(R.id.examplelayout);


         exampleButton = new TextView[exampleListCount];

         LinearLayout.LayoutParams rowsLayoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
         rowsLayoutParams.gravity = Gravity.CENTER;
         rowsLayoutParams.setMargins(10, 10, 10, 0);

         LinearLayout.LayoutParams exampleButtonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
         exampleButtonParams.setMargins(10, 10, 10, 0);

         exampleRowLayoutArray = new LinearLayout[rowsNeededInt];

         int exampleAddedCount = 0;
         for(int x = 0; x < rowsNeededInt; x++){
             exampleRowLayoutArray[x] = new LinearLayout(this);
             exampleRowLayoutArray[x].setOrientation(LinearLayout.HORIZONTAL);
             exampleRowLayoutArray[x].setLayoutParams(rowsLayoutParams);

             for(int i =0; i < 6; i++){

                if(exampleAddedCount < examplerListCount){

                    String exampleNo = String.valueOf(exampleList.get(exampleAddedCount));

                    exampleButton[exampleAddedCount] = new TextView(this);
                    exampleButton[exampleAddedCount].setId(exampleAddedCount+1);
                    exampleButton[exampleAddedCount].setGravity(Gravity.CENTER);
                    exampleButton[exampleAddedCount].setText(exampleNo);
                    exampleButton[exampleAddedCount].setLayoutParams(exampleButtonParams);
                    exampleButton[exampleAddedCount].setTextColor(getResources().getColor(android.R.color.white));
                    exampleButton[exampleAddedCount].setTextSize(TypedValue.COMPLEX_UNIT_DIP, 24);
                    exampleButton[exampleAddedCount].setBackgroundDrawable(new BitmapDrawable(getResources(), ButtonNormal));

                    exampleRowLayoutArray[x].addView(exampleButton[exampleAddedCount]);
                    exampleAddedCount++;
                }

             }


             exampleLayout.addView(exampleRowLayoutArray[x]);

         }

XML

<ScrollView 
        android:id="@+id/examplescrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:layout_below="@+id/topbar">

    <RelativeLayout
        android:id="@+id/examplelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">
        </RelativeLayout>

   </ScrollView>

1 个答案:

答案 0 :(得分:2)

您似乎不需要RelativeLayout的相对布局属性,因此最简单的方法是将其替换为垂直LinearLayout。或者,如果您需要滚动项目列表,请考虑ListView

RelativeLayout中,您可以使用以下内容获得与垂直LinearLayout相同的效果:

  • 为每个孩子生成一个id

  • 使用引用前一个孩子的RelativeLayout.LayoutParams规则创建LAYOUT_BELOW

  • 将布局参数指定给孩子。

相关问题