2017-04-04 54 views
0

我面臨着視圖之間的空間問題。使用LinerLayout並動態添加視圖(ImageViews)到線性佈局。如何在佈局中重疊圖像瀏覽

我想調整imageview之間的空間。我希望他們有一個重疊 - 當添加一個新的視圖。

Current view of the app

目前藍色回地面施加到突出兩個線性佈局。

代碼圖像視圖添加到現有佈局

dealerImages= (LinearLayout) findViewById(R.id.dealerImages); 
    dealerImages.setBackgroundColor(Color.BLUE); 

ImageView view = new ImageView(BlackJack.this); 
    view.setImageResource(R.drawable.back); 
    dealerImages.addView(view); 

我每次添加一個新的觀點 - 我想指定的舊觀點的相對位置。我希望新視圖從佈局中最後一個視圖的中心開始。

請讓我知道你是否需要任何額外的細節。請建議我是否需要使用其他佈局來簡化操作。

編輯 - 在這裏

playerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT); 
    dealerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT); 


private void dealTwoCardsEach(){ 
    player.addCard(hit()); 
    ImageView imageView = new ImageView(BlackJack.this); 
    imageView.setImageResource(getResourceId(player.getFirstCard())); 
    playerImages.addView(imageView, player.getCards().size()-1); 

    dealer.addCard(hit()); 
    imageView = new ImageView(BlackJack.this); 
    imageView.setImageResource(getResourceId(dealer.getFirstCard())); 
    dealerImages.addView(imageView); 

    player.addCard(hit()); 
    if(player.getCount() == 21) 
     player.setBlackJack(true); 

    imageView = new ImageView(BlackJack.this); 
    imageView.setImageResource(getResourceId(player.getSecondCard())); 
    updateMarginForPlayer(); // updating start margin 
    playerImages.addView(imageView, player.getCards().size()-1, playerLayoutParams); 

    dealer.addCard(hit()); 
} 


private void updateMarginForPlayer(){ 
     playerLayoutParams.setMarginStart(playerLayoutParams.getMarginStart()+100); 
} 

發佈的代碼請不我沒有設置第一卡播放器的任何保證金。 我可以看到這兩個卡,直到這裏。以0開始餘量,並用100

private void handleHit(){ 
    Card c1 = hit(); 
    player.addCard(c1); 

    ImageView imageView = new ImageView(this); 
    imageView.setImageResource(getResourceId(c1)); 
    updateMarginForPlayer(); 
    playerImages.addView(imageView, playerLayoutParams); 

} 

開始餘量在「命中」按鈕點擊所述第二圖像的第一圖像是當handleHit()被調用。並且添加的新圖像使得從第2個到當前視圖的所有圖像都不可見。我只能看到第一個和最後一個(最新添加的)。

回答

0

A LinearLayout一個接一個地以線性方式堆疊視圖。你永遠不會創建一個只有LinearLayout的重疊佈局,它總是會根據它定義的方向將它堆疊在當前視圖旁邊。

你正在尋找的是RelativeLayout在相對佈局,視圖堆疊相互依賴,因爲它也是一個ViewGroup類相同的代碼也適用於它。

檢查相對佈局文檔here和他們比較簡短後here

此外,當您創建一個孩子相對佈局,您可以添加PARAMS到哪裏就有奇蹟或只需添加一個保證金是一半大小您的原始佈局將它們從中心堆疊起來!

您不必調用更新參數,而是將params添加到imageview。

ImageView imageView = new ImageView(this); 
    imageView.setImageResource(getResourceId(c1)); 
     RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    layoutParams.setMargins(100,0,0,0); 
    imageView.setLayoutParams(layoutParams); 

    playerImages.addView(imageView, playerLayoutParams); 
+0

謝謝。這似乎工作。但現在,在指定的邊界添加新視圖後,我無法看到它下面的視圖。我只能看到第一個和最新的。是否有一個屬性需要在添加子視圖之前設置爲始終可見。 – Lochan

+0

它們不可見,因爲它們可能在最後一個視圖下方,您可以發佈您的代碼,以便我可以爲您編輯它! – MadScientist

+0

我用代碼編輯了原始問題。請看一下。 – Lochan