【发布时间】:2015-09-30 08:14:48
【问题描述】:
我必须在显示项目列表的 android 屏幕中动态创建布局。 我知道我可以使用 ListView 和 Custom Adapter 来做到这一点,但列表中的项目必须根据它们的数据以不同的方式显示,我想知道我是否可以通过代码生成布局。
我正在尝试这种方式,但我只能看到屏幕上的最后一个项目......似乎其他项目被最后一个抑制了。
这是我的屏幕代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp">
<RelativeLayout
android:id="@+id/allItemsLoadingPanel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true" />
</RelativeLayout>
<LinearLayout
android:id="@+id/allItemsContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dip" />
</LinearLayout>
<com.refractored.fab.FloatingActionButton
android:id="@+id/allItemsFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="66dp"
android:src="@drawable/ic_action_add"
android:visibility="gone"
fab:fab_size="normal"
fab:fab_colorNormal="@color/appColorPrimary"
fab:fab_colorPressed="@color/appColorPrimaryPressed"
fab:fab_colorRipple="@color/appColorPrimaryPressed" />
</FrameLayout>
这是我膨胀并添加到名为“allItemsContainer”的 LynearLayout 的单个项目的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<TextView
android:id="@+id/allItemsIteTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:gravity="center_vertical"
android:textStyle="bold"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/allItemsIteUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="@+id/allItemsIteUsername"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingRight="10dp" />
<TextView
android:id="@+id/allItemsIteText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_below="@+id/allItemsIteTitle"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingRight="10dp" />
<RatingBar android:id="@+id/allItemsIteRatingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="0.1"
android:layout_below="@+id/allItemsIteText"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:scaleX="0.5"
android:scaleY="0.5"
android:layout_marginRight="-50dp"/>
<RelativeLayout
android:id="@+id/allItemsComments"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
在进入项目的循环结束时,我向 lynearLayout 添加了一些项目,但我只能看到一个...... 你能帮帮我吗?
【问题讨论】:
-
发布你尝试过的代码...
-
先用搜索怎么样?!有成千上万的解决方案......这是其中之一stackoverflow.com/a/19065951/284308
-
我尝试在网络上搜索,并找到了一些我尝试实施的解决方案,但结果是相同的。我有一个线性布局(方向垂直),我必须填充它以膨胀一些不同的内容。在进入我的项目列表的循环结束时,我可以看到父 LinearLayout 具有我添加的子项(查看属性 LinearLayout.ChildCount),但我只能在屏幕上看到最后一个
-
那么你有多少种“不同的方式”呢?是 3 还是 5?还是真的很大?
-
该列表可能包含许多项目,也包括 50 或 100 个。我的问题是这是一个“帖子”列表(例如 facebook),其中一些可以显示一些 cmets 并使用列表视图因为这样做可能会更复杂。我认为循环我的项目并动态生成布局可能是一个好方法,但我没有从我的布局中考虑这种行为......