2015-09-30 122 views
0

我必須在android屏幕中動態地創建佈局,以顯示項目列表。 我知道我可以使用ListView和自定義適配器來做到這一點,但列表中的項目必須根據他們的數據以不同的方式顯示,我想知道是否可以通過代碼生成佈局。Android動態生成佈局

我正在嘗試這種方式,但我只能看到最後一個項目進入我的屏幕......好像其他被最後一個壓制。

這裏是我的屏幕代碼:

<?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> 

這裏就是我充氣,並加入到LynearLayout稱爲「allItemsContainer」單個項目的佈局:

<?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,但我只能看到一個... 你能幫我嗎?

+0

後的代碼你嘗試過什麼......關於第一個用搜索 – Pavan

+0

什麼?有數以千計的解決方案在那裏...這裏是其中之一http://stackoverflow.com/a/19065951/284308 – Prexx

+0

我試圖在網上搜索,我發現了一些我試圖實現的解決方案,但結果是相同。 我有一個linearlayout(方向垂直),我必須填充它膨脹一些不同的內容。在我的項目列表循環結束時,我可以看到父LinearLayout有我添加的子項(查看屬性LinearLayout.ChildCount),但我只能看到屏幕上的最後一個 – Badozvora

回答

0

是的,你可以從代碼生成佈局 你可以在佈局中定義一個LinearLayout。通過使用.addView(圖)

防爆添加您欣賞到的LinearLayout:

TextView textView = new TextView(this) 
linearLayout.addView(textView); 
0

傢伙我解決了它:我的代碼是正確的,但在我的情況下,我加入一個滾動型爲母我的「allItemsContainer」LinearLayout。現在一切正常。 這裏是我的屏幕上的代碼現在:

<?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> 
     <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <LinearLayout 
       android:id="@+id/allItemsContainer" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:paddingTop="10dip" /> 
     </ScrollView> 
    </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>