2012-05-09 70 views
0

在我的代碼中,我在圖片下方添加了圖片和文本。我有一個帶有RelativeLayout的XML,它包含一個ImageView和一個TextView。 動態我膨脹這個XML佈局,並將其添加到根XML中的LinearLayout。我面臨的問題是,如果我添加更多的圖片,屏幕可以容納(寬度),最後一張圖片變小。我希望能夠擁有相同的結構並添加一個新的行,例如我希望每行都有最多4張圖片,我該如何實現?Android - 動態添加多個視圖

下面是我的根XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
<TextView 
    android:id="@+id/text" 
    android:text="Friends who likes this:" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true"/> 
<LinearLayout 
    android:id="@+id/wrapper" 
    android:layout_below="@+id/text" 
    android:layout_marginTop="5dip" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:orientation="horizontal"/> 
</RelativeLayout> 

這裏是我的XML佈局,其中我有ImageView的和TextView的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:id="@+id/root" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
<ImageView 
    android:id="@+id/image" 
    android:layout_width="35dip" 
    android:layout_height="35dip" 
    android:layout_marginLeft="3dip" 
    android:src="@drawable/default_profile_picture"/> 
<TextView 
    android:id="@+id/username" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/image" 
    android:layout_marginTop="2dip" 
    android:layout_marginLeft="3dip" 
    android:textSize="9sp" 
    android:text="Damian M."/> 
</RelativeLayout> 

和Java代碼膨脹這樣的:

LayoutInflater inflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.likes_view, this, true); 
    LinearLayout wrapper = (LinearLayout) findViewById(R.id.wrapper); 
    TextView text = (TextView) findViewById(R.id.text); 
    text.setTypeface(tf); 
    if(users.isEmpty()){ 
     text.setVisibility(View.INVISIBLE); 
    } 

    RelativeLayout friendsView; 
    ImageView image; 
    TextView username; 
    for(User user : users){ 
     friendsView = (RelativeLayout) View.inflate(ctx, R.layout.likes_view_friends, null); 
     image = (ImageView) friendsView.findViewById(R.id.image); 
     if(user.getImage() != null){ 
      image.setImageBitmap(user.getImage()); 
     } 
     username = (TextView) friendsView.findViewById(R.id.username); 
     username.setTypeface(tf); 
     String firstLetterLastName = user.getLastName().substring(0,1); 
     StringBuilder dotLastName = new StringBuilder(firstLetterLastName).append(".");   
     username.setText(user.getFirstName() + " " + dotLastName.toString()); 
     wrapper.addView(friendsView); 
    } 

回答

0

考慮使用GridView。 Android開發者資源提供了一個教程Hello Views

+0

我忘了說,我在listview中做這個。我可以把GridView放在ListView中嗎?! – Carnal

+0

是的。不過,您將不得不管理2個適配器。一個用於ListView,一個用於GridView。 – Rajesh

+0

我明白你的意思了。但我希望GridView充當ListView中的項目,因此當我向下滾動ListView時,圖片也應該與ListView一起滾動。你的建議會導致我有兩個滾動視圖,一個用於listview,另一個用於gridview。 – Carnal