Android 线性布局重叠
由于某些原因,我在ScrollView中添加到RelativeLayout的线性布局相互重叠。 我假设有某种方法可以设置线性布局,以便他们将自己定位在上面的线性布局之下Android 线性布局重叠,android,android-linearlayout,Android,Android Linearlayout,由于某些原因,我在ScrollView中添加到RelativeLayout的线性布局相互重叠。 我假设有某种方法可以设置线性布局,以便他们将自己定位在上面的线性布局之下 exampleLayout = (RelativeLayout)findViewById(R.id.examplelayout); exampleButton = new TextView[exampleListCount]; LinearLayout.LayoutParams row
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]);
}
exampleLayout=(RelativeLayout)findViewById(R.id.exampleLayout);
exampleButton=新文本视图[exampleListCount];
LinearLayout.LayoutParams rowsLayoutParams=新建LinearLayout.LayoutParams(LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容);
rowsLayoutParams.gravity=gravity.CENTER;
rowsLayoutParams.setMargins(10,10,10,0);
LinearLayout.LayoutParams exampleButtonParams=新建LinearLayout.LayoutParams(LayoutParams.WRAP\u内容,LayoutParams.WRAP\u内容);
设置边距(10,10,10,0);
示例RowLayoutarray=新的线性布局[RowSneedInt];
int exampleAddeCount=0;
对于(int x=0;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>
您似乎不需要任何东西都使用
RelativeLayout
的相对布局属性,因此最简单的方法是将其替换为垂直线性布局。或者如果你需要一个滚动项目列表,考虑一个<代码> ListVIEW < /C>
在相对布局
中,您可以通过以下操作实现与垂直线性布局
相同的效果:
- 为每个孩子生成一个id
- 创建
RelativeLayout.LayoutParams
,使用下面的LAYOUT_
规则引用上一个子项
- 将布局参数指定给子级
如果您使用的是线性布局阵列,为什么不使用ListView而不是相对布局?我使用多个水平线性布局来替代Android 2.3.3中不可用的GridLayout。我尝试过使用支持库,但未能使其正常工作。我在ScrollView内部使用RelativeLayout,因为ScrollView只能有一个直接子项。还有GridView
。我将查看GridView。但是必须有一些简单的方法让线性布局显示在彼此下方。是的,将它们放置在垂直的线性布局中。谢谢!你是个传奇人物。