2014-05-11 30 views
0

我試圖實現類似的結果:類似於人的動態佈局應用程式

http://cdn7.staztic.com/app/a/2541/2541494/share-my-contact-unlocker-2-0-s-307x512.jpg

凡接觸可能會或可能不會有電子郵件地址或其他個人資料。是否有可能通過XML來實現其大部分,還是都需要通過Java來完成?

這裏是我想象將是一個溶液中的僞代碼:

如果(contact.hasPhoneNumber)在接觸

的foreach數

膨脹r.layout.contact_number

的setText PHONENUMBER textview

添加視圖到佈局

是否有更智能的方法?

+0

如果你想要一個幾乎純粹的XML的方法,你可以每寫你的XML查看你可能需要,然後在你不需要它們時隱藏它們。 –

回答

0

這是根據解決如下:

<ScrollView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/contactContent" 
    android:layout_below="@+id/profile_background"> 
    <LinearLayout android:orientation="vertical" android:id="@+id/c_detail_content_main" android:paddingBottom="50.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content"> 
     <LinearLayout android:orientation="vertical" android:visibility="gone" android:id="@+id/c_detail_birthday_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20.0dip"> 
</LinearLayout 
</ScrollView> 

以下代碼:

if (this.contact.hasPhoneNumber()) 
    { 
     LinearLayout phoneNumberContent = (LinearLayout) view.findViewById(R.id.c_detail_phone_layout); 
     phoneNumberContent.setVisibility(View.VISIBLE); 
     for (PhoneNumber number : this.contact.getPhoneNumbers()) 
     { 
      LinearLayout tempNumberContent = new LinearLayout(this.getActivity().getApplicationContext()); 
      TextView type = new TextView(this.getActivity().getApplicationContext()); 
      TextView phoneNo = new TextView(this.getActivity().getApplicationContext()); 
      type.setWidth(LayoutTools.getDPMeasurement(this.getActivity(), 85)); 
      phoneNo.setText(number.getNumber()); 
      tempNumberContent.addView(type); 
      tempNumberContent.addView(phoneNo); 
      phoneNumberContent.addView(tempNumberContent); 
     } 
    } 
相關問題