2013-12-11 70 views
2

我想如下創建活動的佈局: enter image description here添加垂直和水平滾動編程到的LinearLayout Android中

game_board.xml代碼是如下:

<LinearLayout 
     android:id="@+id/layoutFilterContent" 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:weightSum="4" 
     android:background="#837E7C" > 

     <LinearLayout 
      android:id="@+id/layoutTopicGrade" 
      android:orientation="vertical" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:padding="0dp" 
      android:background="#BCC6CC" > 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/layoutGames" 
      android:orientation="vertical" 
      android:layout_width="0dp" 
      android:layout_height="fill_parent" 
      android:layout_weight="3" 
      android:padding="0dp" 
      android:background="#E5E4E2" > 

     </LinearLayout> 

    </LinearLayout> 

java代碼在GameBoardActivity.java如下:

import android.os.Bundle; 
import android.annotation.SuppressLint; 
import android.app.ActionBar.LayoutParams; 
import android.app.Activity; 
import android.view.Menu; 
import android.widget.ImageButton; 
import android.widget.LinearLayout; 
import android.widget.ScrollView; 

public class GameBoardActivity extends Activity { 

    LinearLayout topicGrade; 
    LinearLayout gameContent; 
    ScrollView scroll; 
    ImageButton[] imgBtn; 

    @SuppressWarnings("deprecation") 
    @SuppressLint({ "NewApi", "InlinedApi", "ResourceAsColor" }) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.game_board); 

     topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade); 

     scroll = new ScrollView(this); 
     scroll.setBackgroundColor(android.R.color.transparent); 
     scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
     scroll.addView(topicGrade); 

     imgBtn = new ImageButton[15]; 

     for(int i=0;i<15;i++){ 
      imgBtn[i] = new ImageButton(this); 
      imgBtn[i].setId(i); 
      imgBtn[i].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT)); 
      topicGrade.addView(imgBtn[i]); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.game_board, menu); 
     return true; 
    } 

} 

我得到error作爲:The specified child already has a parent. You must call removeView() on the child's parent first.

我第一次嘗試添加按鈕到LinearLayout1沒有ScrollView,我可以添加按鈕,因爲我想,但是當我執行ScrollView我得到上述錯誤。所以,我被困,並沒有嘗試任何事情上LinearLayout2

請人幫我設計上面顯示的活動,

+0

我加你需要我的回答 – 2red13

回答

1

你想做些什麼?這是你真正做什麼:

setContentView(R.layout.game_board); 

- > game_board是inflatet與兩個孩子的layoutTopicGrade和layoutGames

topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade); 

- >這是layoutTopicGrade

的實例
scroll = new ScrollView(this); 

- >您創建滾動視圖

scroll.addView(topicGrade); 

- >添加topicGrade滾動視圖

這失敗,因爲topicGrade已經有父(layoutFilterContent)

this.setContentView(scroll); 

- >設置了克羅爾查看內容。
(爲什麼你之前設置R.layout.game_board的內容?)

如果只想topicGrade在ScrollViewContentView,你爲什麼不建立這種結構的XML?

如果建議做你喜歡做的,解決的辦法是:

topicGrade = (LinearLayout)findViewById(R.id.layoutTopicGrade); 
((LinearLayout)topicGrade.getParent()).removeView(topicGrade) 

,但我看不出爲什麼你應該以這種方式^^

--edit做到這一點: 現在我知道了,這裏是你需要:

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

<ScrollView 
    android:id="@+id/scrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="1"> 
    <LinearLayout 
     android:id="@+id/vertical_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 
    </LinearLayout> 
</ScrollView> 

<HorizontalScrollView 
    android:id="@+id/horizontalScrollView1" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_weight="3"> 

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

謝謝你的迴應。行'this.setContentView(scroll);'是錯誤添加的,我刪除了這一行。我想把'活動'分成兩部分(如果4是總數,那麼就是1/4和3/4)',正如我在我的問題中提到的,你可以在我提供的圖像中看到佈局。我怎樣才能通過'Horizo​​ntal Scroll'和'Horizo​​ntal Scroll'將佈局劃分爲'1/4'? – Sush19

1

每個元素都有一個父,這是一個XML文件結構。

在您的layout xml文件中,您將layoutTopicGrade定義爲layoutFilterContent的子項。然後,在代碼中,您將layoutTopicGrade設置爲新創建的ScrollView的子項。

你應該包裝你的layoutTopicGradeScrollView。此外,由於你沒有做任何動態配置到ScrollView,沒有理由在代碼中加入它,你應該這樣做直接在佈局XML文件:

<ScrollView android:id="@+id/scrollViewTopicGrade" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="@android:color/transparent" > 
    <LinearLayout 
     android:id="@+id/layoutTopicGrade" 
     android:orientation="vertical" 
     android:layout_width="0dp" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:padding="0dp" 
     android:background="#BCC6CC" > 
    </LinearLayout> 
</ScrollView> 

編輯

爲了具有layoutTopicGrade採取父母的寬度的25%和layoutGames取75%,執行以下操作:

  1. 中號將layout_weightlayout_widthlayoutTopicGrade改爲ScrollView
  2. Set ScrollViewlayout_weight.25
  3. 設置layoutGameslayout_weight.75
  4. layoutTopicGrade的孩子應該有layout_width設置爲fill_parent

請參閱this answer


加成

順便說一句,貌似線this.setContentView(scroll);應予以刪除。 Activity.setContentView用於爲整個活動=整個屏幕設置視圖。但你的ScrolView顯然只適用於主題佈局。相反,整個遊戲板就是活動的內容視圖,因爲您之前使用它的幾行代碼:setContentView(R.layout.game_board);(保留那個)。

+0

的XML謝謝您的答覆。行'this.setContentView(scroll);'是錯誤添加的,我刪除了這一行。正如你所說的,直接使用''到'xml'。如果我直接在'xml'中使用'',我已經在問題中發佈了,我不會得到兩個並排佈局。 – Sush19

+0

@ Sush19無論如何,你看看它,你可以也應該在XML中做到這一點。你嘗試將權重移動到ScrollView嗎? – yair

+0

@ Sush19(1)將'layout_weight'和'layout_width'從'layoutTopicGrade'移動到'ScrollView'。 (2)將'ScrollView'的'layout_weight'設置爲'.25'。 (3)將'layoutGames'的'layout_weight'設置爲'.75'。 (4)'layoutTopicGrade'的孩子應該'layout_width'設置爲'fill_parent'。 – yair