【发布时间】:2015-03-23 07:07:31
【问题描述】:
我是 android 编程新手。我想在滚动视图中实现元素并排的简单布局。这个想法是每次处理一个带有图像和文本的元素,让布局根据屏幕分辨率选择回车的正确时间。我尝试了每一种布局,但似乎没有一种适合我的目的。特别是相对布局元素重叠,而不是我需要的是空间附加。在尝试解决方法之前(例如在线性布局中连续添加更多元素),我想知道是否存在更自然的解决方案。
(来源: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?? -
会自动识别回车吗?
-
是的。您必须将所有值添加到列表中,然后使用
CustomAdapter设置它们
标签: android layout scrollview android-relativelayout