android为元素并排的布局

android为元素并排的布局,android,layout,scrollview,android-relativelayout,Android,Layout,Scrollview,Android Relativelayout,我是安卓程序新手。我想在scrollview中实现元素并排的简单布局。其思想是每次处理一个带有图像和文本的元素,根据屏幕分辨率,让布局选择何时是回车的正确时间。我尝试了各种布局,但似乎没有一种适合我的目的。特别是与相对布局元素重叠,而我需要的是一个空间附加。在尝试解决方法(例如在线性布局中的行中添加更多元素)之前,我想知道是否存在更自然的解决方案 (来源:) 我创建了一个示例活动来尝试解决方案: public class MainActivity extends ActionBarAct

我是安卓程序新手。我想在scrollview中实现元素并排的简单布局。其思想是每次处理一个带有图像和文本的元素,根据屏幕分辨率,让布局选择何时是回车的正确时间。我尝试了各种布局,但似乎没有一种适合我的目的。特别是与相对布局元素重叠,而我需要的是一个空间附加。在尝试解决方法(例如在线性布局中的行中添加更多元素)之前,我想知道是否存在更自然的解决方案


(来源:)

我创建了一个示例活动来尝试解决方案:

   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>
公共类MainActivity扩展了ActionBarActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终RelativeLayout容器=(RelativeLayout)findViewById(R.id.container);
对于(int i=0;i<100;i++)
{
最终按钮=新按钮(此按钮);
按钮设置文本(“sda”+i);
按钮。设置ID(i);
container.addView(按钮);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
容器。移除视图(按钮);
}
});
}
}
}

对于显示的图表,您可以选择网格布局,您可以自定义单元格间距的网格布局。 如果它仍然不适合您的需要,那么我的建议是使用布局权重的线性布局,但是嵌套权重是一种性能开销


希望这有帮助

从上图中,您可以使用GridView绘制给定的UI。您可以指定项目之间的间距,也可以指定每行包含多少列。 查看开发者文档以供参考


为什么不使用
gridView
?它是否自动识别回车?是的。您必须将所有值添加到列表中,然后使用
CustomAdapter
是否为自动滚动网格设置它们?所以我不需要父滚动视图?不,你不需要父滚动视图。网格是可滚动的。只需将网格项目充气,它将根据适配器进行充气。有关更多信息,请查看文档。