我是android编程的新手。我想实现一个简单的布局,使元素在scrollview中并排。想法是每次用图像和文本处理单个元素,让布局根据屏幕分辨率选择何时返回正确的笔架时间。我尝试了每种布局,但似乎没有一种适合我的目的。特别是相对布局元素重叠,而我需要的是空间附加。在尝试workaroud之前(例如,在线性布局中连续添加更多元素),我想知道是否存在更自然的解决方案。
(来源:youth-stories.com)
我创建了一个示例活动来尝试解决方案:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RelativeLayout container = (RelativeLayout)findViewById(R.id.container);
for(int i=0; i < 100; i++)
{
final Button button = new Button(this);
button.setText("sda"+i);
button.setId(i);
container.addView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
container.removeView(button);
}
});
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:id="@+id/outer"
android:tileMode="disabled" android:gravity="top">
<ImageView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/background"
android:scaleType="centerCrop"/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/scrollView" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/container"></RelativeLayout>
</ScrollView>
</RelativeLayout>
从上面给出的图,您可以使用GridView绘制给定的UI。您可以指定项目之间的间距以及每行包含多少列。供参考,请参阅开发人员文档。
对于所示的图表,您可以使用网格布局,可以自定义网格布局以适应单元格之间的间距。如果仍然不能满足您的需求,我的建议是使用具有布局权重的线性布局,但是嵌套权重会带来性能开销。
http://developer.android.com/reference/android/widget/GridLayout.html
Why are nested weights bad for performance? Alternatives?
希望获得帮助!