2013-10-04 50 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