【问题标题】:android which layout for elements side by sideandroid 元素的并排布局
【发布时间】: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


【解决方案1】:

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

Check here for GridView example and doc

【讨论】:

  • 是自动滚动的网格吗?所以我不需要父 scrollView?
  • 不,你不需要父滚动视图。网格是可滚动的。只需膨胀网格项目,它将根据适配器填充。有关更多信息,请查看文档。
【解决方案2】:

对于显示的图表,您可以使用网格布局,您可以为单元格之间的间距自定义网格布局。 如果它仍然不能满足您的需要,我的建议是带有布局权重的线性布局,但是嵌套权重是性能开销。

http://developer.android.com/reference/android/widget/GridLayout.html

http://androidexample.com/Custom_Grid_Layout_-_Android_Example/index.php?view=article_discription&aid=76&aaid=100

Why are nested weights bad for performance? Alternatives?

希望有帮助!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 2021-09-17
    相关资源
    最近更新 更多