2013-10-04 177 views
0

所以我試圖獲得視圖的組合建立。他們必須不斷地有按鈕EDITTEXT箱體頂部水平彼此相鄰並低於的一個垂直列表Textviews。垂直列表應包含在一個滾動型允許用戶向下穿過TextViews滾動(該按鈕的EditText頂部應仍是可見的,而這種情況正在發生)。滾動視圖內部的水平和垂直線性佈局

protected void initLayout() { 
    // Declaring the vertical layout 
    verticalLayout=new LinearLayout(this); 
    verticalLayout.setOrientation(LinearLayout.VERTICAL); 
      //Declaring the horizontal layout 
    horizontalLayout=new LinearLayout(this); 
    horizontalLayout.setOrientation(LinearLayout.HORIZONTAL); 
      //set the main view as horizontal at the top 
    setContentView(horizontalLayout); 
      //Declaring the scroll view 
    ScrollView scrollView= new ScrollView(this); 
    scrollView.addView(verticalLayout); 
      //set the scroll view 
    setContentView(scrollView); 
    //declare and add button to horizontal view 
    theButton= new Button(this); 
    theButton.setText("Add Joke"); 
    horizontalLayout.addView(theButton); 
    //declare and add edittext to horizontal view 
    theEditText= new EditText(this); 
    theEditText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)); 
    horizontalLayout.addView(theEditText); 
} 

我相信我可能會出錯setContentView,但不完全確定。如果有人有任何意見,將不勝感激

+0

歡迎來到SO。你的代碼出了什麼問題?你得到的結果是什麼,對於預期的結果有多不同? – dic19

+0

謝謝。代碼正在顯示編輯文本,但按鈕和編輯文本並未顯示。預期的結果是edittext和按鈕位於頂部並停留在那裏,因爲用戶滾動文本瀏覽 – algorhythm

回答

0

我找到了解決問題的辦法。我不得不在另一個線性佈局中封裝水平佈局和垂直佈局,並將其設置爲根視圖。

protected void initLayout() { 
    // Declaring the vertical layout 
    verticalLayout=new LinearLayout(this); 
    verticalLayout.setOrientation(LinearLayout.VERTICAL); 

    //Declaring the horizontal layout 
    horizontalLayout=new LinearLayout(this); 
    horizontalLayout.setOrientation(LinearLayout.HORIZONTAL); 

    //***SOLUTION*** Create a view to group the other views in 
    groupLayout=new LinearLayout(this); 
    groupLayout.setOrientation(LinearLayout.VERTICAL);//vertical as the horizontal view is on top of the vertical view 

    //Declaring the scroll view 
    ScrollView scrollView= new ScrollView(this); 
    scrollView.addView(verticalLayout);//the vertical layout is the only view that should be scrollable and therefore added to the scrollview 

    //declare and add button to horizontal view 
    theButton= new Button(this); 
    theButton.setText("Add Joke"); 
    horizontalLayout.addView(theButton); 
    //declare and add edittext to horizontal view 
    theEditText= new EditText(this); 
    theEditText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)); 
    horizontalLayout.addView(theEditText); 

    //***SOLUTION*** attach the views to the group view 
    groupLayout.addView(horizontalLayout); //the layout displayed at the top of the group layout 
    groupLayout.addView(scrollView); //the layout below the top (scrollview already contains the vertical view) 

    setContentView(groupLayout);//assign the group layout to the content 
} 
0

你的父母ViewGroup必須是一個。你正在使用2次setContentView,這是你的錯誤。只使用一個,並在另一箇中添加其他線性佈局作爲子項。 我認爲你最好在xml中嘗試做,然後編寫你的代碼。

+0

如何設置視圖組以使水平佈局總是出現在頂部並且垂直佈局在下面水平佈局但可以滾動瀏覽? – algorhythm

0

基本上你已經得到了錯誤的setContentView ...
我強烈建議在XML來定義佈局 - 然後用下面的設置它只是一次:

setContentView(R.layout.my_xml_layout); 

這種方式 - 通常你確實有可能看到你的佈局,並且它是(在我看來)easyer來定義一個佈局(特別是如果你決定改變這一天)。

,如果你wan't做代碼然而,你必須做的就是像下面這樣(未經測試 - 但應該工作)

protected void initLayout() { 
// Declaring the vertical layout 
verticalLayout=new LinearLayout(this); 
verticalLayout.setOrientation(LinearLayout.VERTICAL); 

//Declaring the horizontal layout 
horizontalLayout=new LinearLayout(this); 
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL); 
verticalLayout.addView(horizontalLayout); //add the Horizontal-View to the parent-grid 

//Declaring the scroll view 
ScrollView scrollView= new ScrollView(this); 
scrollView.addView(verticalLayout); 
//set the scroll view 
verticalLayout.addView(scrollView); //add the scrollview to the parent-grid 
//declare and add button to horizontal view 
theButton= new Button(this); 
theButton.setText("Add Joke"); 
horizontalLayout.addView(theButton); 
//declare and add edittext to horizontal view 
theEditText= new EditText(this); 
theEditText.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT)); 
horizontalLayout.addView(theEditText); 
setContentView(verticalLayout); 
} 

編輯:對不起 - 有幾個錯誤第一次 - 我現在編輯它,它應該像這樣工作。

+0

感謝您的幫助。您的建議幫助我獲得了我想要的視圖(頂部的按鈕和編輯文本),但滾動視圖不允許我滾動垂直佈局中的文本視圖。我想也許你的編輯代碼的第15行和第17行有一個問題,它似乎將sccrollview和垂直佈局添加到彼此。如果你可以提供更好的建議 – algorhythm

+0

啊它滾動正常,但我遇到的問題是當滾動瀏覽全文時,水平佈局滾動出視圖。水平佈局是否可以在滾動垂直佈局的同時停留在屏幕的頂部? – algorhythm