Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式在布局中布局子视图_Android_Android Layout_Android Relativelayout - Fatal编程技术网

Android 以编程方式在布局中布局子视图

Android 以编程方式在布局中布局子视图,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我试图在平板电脑上以相对布局布局视图,就像书架一样。将有这么多跨,然后创建一个新行 我的初始状态如下所示: <ScrollView android:layout_marginTop="150sp" android:layout_marginLeft="50sp" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayou

我试图在平板电脑上以相对布局布局视图,就像书架一样。将有这么多跨,然后创建一个新行

我的初始状态如下所示:

  <ScrollView
    android:layout_marginTop="150sp"
    android:layout_marginLeft="50sp"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RelativeLayout
      android:id="@+id/user_list"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">

      <!-- Add User Avatar -->
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:src="@drawable/user_frame" />
            <ImageView
                android:id="@+id/demo_image"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:src="@drawable/add_user"
                android:layout_marginLeft="10px" />
            <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:src="@drawable/user_name_background"
                android:layout_marginLeft="10px" />
            <TextView
                android:id="@+id/user_name"
                android:layout_width="180px"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textStyle="bold"
                android:gravity="center_horizontal"
                android:text="Add New User" />
        </RelativeLayout>

      </RelativeLayout>
  </ScrollView>

我正在从数据库中获取记录,对于每个记录,我需要放置一个版本的“添加用户化身”部分,如上所示。我想做5次,然后换行

我最初的想法是使用布局充气器并以编程方式添加视图,使用RelativeLayout.LayoutParams设置布局值


但一定有更简单的方法。我见过很多应用程序使用“书架”这个比喻。

我可以想到一些选择

  • 与其使用相对布局,为什么不使用带有水平属性的LinearLayouts
  • 使用表格布局,很像线性布局
  • 使用带有自定义适配器的ListView来填充每个ListRow的五个“添加用户化身”

  • 以编程方式布置视图相当简单。我在所有活动/观点中都这样做。我喜欢Android的XML布局概念,但发现一旦视图基于外部数据(如数据库)变得完全动态,它就会变得太复杂

    我选择最外层的布局,然后仅在XML中实例化该布局。然后在我的活动中,我使用普通的findby id调用查找外部布局,然后使用一系列add(View)方法调用根据需要添加动态创建的视图或布局

    您需要考虑不同的屏幕方向、像素密度和大小。与设备无关的像素将成为您的朋友。例如,要创建图像视图,您需要加载位图,确定其大小(如果需要),然后将其设置为新ImageView实例的可绘制对象,然后将其添加到布局中


    听起来您的外部布局将是一个滚动视图,内部为垂直线性布局,然后每个“书架”都有一个水平布局,每个“书”最多有五个图像视图。

    我的第一个想法是使用GridView。