2016-08-31 25 views
-2

我已經創建了一個帶有ImageView的佈局,我想用兩個不同的ImageView 將它包含兩次,就像具有不同位置的ListView。android包括一個不同ID的佈局

這是ImageView的佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent"> 

    <include 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/main_guilds_item" 
     android:layout_gravity="center_horizontal" /> 

    <include 
     layout="@layout/main_guilds_item" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

在這裏,我有佈局的兩倍,但我想訪問不同ID的那些ImageViews來設置不同的圖像。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <include 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/main_guilds_item" 
     android:layout_gravity="center_horizontal" /> 

    <include 
     layout="@layout/main_guilds_item" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" /> 
</LinearLayout> 

我該怎麼辦?

+1

請添加您的代碼 – Stefan

+0

請分享您已有的代碼,並且如果可能,請提供您想要實現的(簡單和小型)圖像。這樣我們其他人就更容易幫助你。 – Marcel50506

+0

您添加的兩個代碼塊是相同的。你可以包含'main_guilds_item'xml佈局嗎? – Marcel50506

回答

0

您可以爲此創建自定義視圖。步驟是

  1. 創建視圖的XML文件my_custom_view.xml

    <RelativeLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    
    <ImageView 
        android:id="@+id/image_view" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:background="@drawable/ic_launcher" 
        /> 
    </RelativeLayout> 
    
  2. 然後,你需要創建一個類ItemList擴展的RelativeLayout(在你的my_custom_view.xml父佈局)

    public class ItemList extends RelativeLayout { 
    
    ImageView mImageView; 
    
    public ItemList(Context context) { 
    this(context, null); 
    } 
    
    public ItemList(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
    } 
    
    public ItemList(Context context, AttributeSet attrs, int defStyle){ 
    super(context, attrs, defStyle); 
    LayoutInflater.from(context).inflate(R.layout.item_list_content, this); 
        mImageView=(ImageView) findViewById(R.id.image_view); 
    } 
    
    public void setImage(Drawable imageId){ 
    mImageView.setImageDrawable(imageId); 
    } 
    } 
    
  3. 然後,您可以在其他佈局中多次使用此自定義視圖,如下所示

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    
        <yourpackage.ItemList 
         android:id="@+id/first_view" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 
        </yourpackage.ItemList> 
    
        <yourpackage.ItemList 
         android:id="@+id/second_view" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent"> 
        </yourpackage.ItemList> 
    
    </LinearLayout> 
    
  4. 現在從你的類,你做

    ItemList firstItem=(ItemList) findViewById(R.id.first_view); 
    ItemList secondItem=(ItemList) findViewById(R.id.second_view); 
    firstItem.setImage(R.drawable.your_image); 
    secondItem.setImage(R.drawable.your_image_2); 
    

注: 您也可以將圖像從XML。在這種情況下,你將不得不使用自定義屬性。按照該目的的鏈接。 https://stackoverflow.com/a/7608739/3975838