滚动视图内的水平和垂直线性布局

时间:2013-10-04 20:50:08

标签: android scrollview android-linearlayout

我正在尝试建立 Views 的组合。他们必须始终在顶部水平旁边的按钮 Edittext 框中,并且在垂直下方 Textviews 列表。垂直列表应包含在 ScrollView 中,以允许用户向下滚动 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出错,但并不完全确定。

3 个答案:

答案 0 :(得分:0)

您的父ViewGroup必须是一个。你正在使用2次setContentView,这是你的错误。只使用一个,并在另一个中添加其他linearlayout作为子项。 我认为你最好尝试在xml中编写代码然后编写代码。

答案 1 :(得分:0)

基本上你有setContentView错误...
我强烈建议在xml中定义布局 - 然后使用以下内容设置一次:

setContentView(R.layout.my_xml_layout);

这种方式 - 你通常可以看到你的布局,并且(在我看来)更容易定义布局(特别是如果你决定改变这一天)。

如果您不想在代码中执行此操作,那么您需要做的就是以下内容(未经测试 - 但应该有效)

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);
}

编辑:抱歉 - 第一次出现了一些错误 - 我现在编辑了它,它应该像这样工作。

答案 2 :(得分: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
}