0

我正在尝试建立视图的组合。他们必须不断地在顶部有一个ButtonEdittext框,它们水平相邻,下方有一个Textviews的垂直列表。垂直列表应包含在ScrollView中,以允许用户向下滚动TextViews(发生这种情况时,顶部的ButtonEditText应该仍然可见)。

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 可能会出错,但我不确定。

4

3 回答 3

0

您的父 ViewGroup 必须是一个。您使用了 2 次 setContentView,这是您的错误。仅使用一个并将其他线性布局添加为另一个中的子级。我认为最好尝试在 xml 中执行,然后像这样编写代码。

于 2013-10-04T20:55:21.510 回答
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);
}

编辑:对不起 - 第一次有一些错误 - 我现在编辑它,它应该像这样工作。

于 2013-10-04T21:01:21.187 回答
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
}
于 2013-10-08T18:33:12.800 回答