2014-09-17 14 views
6

Tôi có 2 file xml như thế này:động tải tập tin XML để LinearLayout

main.xml:

<HorizontalScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:id="@+id/ll" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 
    </LinearLayout> 
</HorizontalScrollView> 

items.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/rl" 
android:layout_width="100dp" 
android:layout_height="100dp"> 

    <ImageView 
    android:id="@+id/img" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_marginLeft="15dp" 
    android:layout_marginTop="15dp" 
    android:contentDescription="@string/app_name" 
    android:gravity="center" 
    android:src="@drawable/icon_shop2" /> 

    <TextView 
    android:id="@+id/providerName" 
    style="@style/WhiteTextMedium" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/img" 
    android:layout_gravity="bottom" 
    android:paddingLeft="10dp" 
    android:text="Example" /> 

</RelativeLayout> 

main.xml được sử dụng cho MainActivity. Câu hỏi của tôi là cách tải động 3 bố cục items.xml vào Linearlayout trong HorizontalScrollView của main.xml!

+1

Làm tăng bố cục mục và thêm từng mục một theo chiều ngang của bạn Xem ... –

+0

Bạn có thể cho tôi mã nguồn cho đến giờ ...! –

+0

@HT Không, tôi không ... Tôi chỉ cung cấp cho bạn một ý tưởng ... Bây giờ thực hiện tìm kiếm trên Google .... bạn sẽ nhận được mã ..... –

Trả lời

10

Hãy thử cách này, hy vọng điều này sẽ giúp bạn giải quyết vấn đề của mình.

LinearLayout ll; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    ll = (LinearLayout) findViewById(R.id.ll); 
    for (int i=0;i<3;i++){ 
     View view = LayoutInflater.from(this).inflate(R.layout.items,null); 
     ImageView imageView = view.findViewById(R.id.img); 
     TextView providerName = view.findViewById(R.id.providerName); 
     // Assigning value to imageview and textview here 
     ll.addView(view); 
} 
+0

Cảm ơn bạn đã trả lời! :) –

+0

Rất vui được giúp bạn, bạn có thể vui lòng upvote ans nếu nó hữu ích cho bạn? –

1

Có hai cách để làm như vậy:

Way to Left (sử dụng xml nếu số lượng các mặt hàng là cố định):

<HorizontalScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:id="@+id/ll" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 

      <include 
      android:id="@+id/item1" 
      layout="@layout/items.xml"/> 

      <include 
      android:id="@+id/item2" 
      layout="@layout/items.xml"/> 

      <include 
      android:id="@+id/item3" 
      layout="@layout/items.xml"/> 

    </LinearLayout> 


</HorizontalScrollView> 

Way Right (Trong Hoạt động sử dụng bố trí Inflator, nếu số mục là động):

setContentView(R.layout.main); 
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

LinearLayout ll = (LinearLayout) findViewById(R.id.ll); 

View v = inflater.inflate(R.layout.items, ll); 

for (short i=0;i<3;i++) 
    ll.addView(v); 
+0

Cảm ơn bạn, Jain! Xin lỗi, tôi không thể bỏ phiếu! –

Các vấn đề liên quan