【发布时间】:2014-03-23 10:25:21
【问题描述】:
对于 Android 应用程序有这样的布局的最佳方式是什么?
当行号为偶数或 0 时,该行应该有 2 列。 否则该行应该只有 1 列
必须动态生成行。 单元格中还应包含图像和一些文本。
我应该使用什么布局:tablelayout 还是 gridlayout?什么观点?
【问题讨论】:
对于 Android 应用程序有这样的布局的最佳方式是什么?
当行号为偶数或 0 时,该行应该有 2 列。 否则该行应该只有 1 列
必须动态生成行。 单元格中还应包含图像和一些文本。
我应该使用什么布局:tablelayout 还是 gridlayout?什么观点?
【问题讨论】:
使用 LinearLayout(垂直)作为父级,使用 LinearLayout(水平作为子级),在您的子级中,您可以根据您的 forloop 计数添加一或两个视图。
伪
rootLL = createLinearLayout(vertical)
for (int i=0;i<10;i++)
{
childLL = createLinearLayout(horizontal)
view1 = createView()
view1.setWeight(1)
childLL.addChild(view1)
if (i%2==0)
{
view2 = createView()
view2.setWeight(1)
childLL.addChild(view2)
}
rootLL.addChild(childLL)
}
【讨论】:
您只需编辑此代码即可实现所需的布局我设计了这个为了您一切顺利您只需将其作为示例,您可以尝试比这更好
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:weightSum="2" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="2" >
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/black"
android:src="@drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:src="@drawable/ic_launcher" />
</LinearLayout>
【讨论】: