簡體   English   中英

滾動視圖內的水平和垂直線性布局

[英]Horizontal and Vertical linear layout inside a scroll view

我正在嘗試建立視圖的組合。 它們必須始終在頂部水平相鄰且在垂直Textview列表下方始終具有一個ButtonEdittext框。 垂直列表應包含在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可能會出錯,但並不確定。

您的父級ViewGroup必須是一個。 您正在使用2次setContentView,這是您的錯誤。 僅使用一個,並將其他linearlayout作為子元素添加到另一個子元素中。 我認為最好嘗試在xml中執行操作,然后像這樣編寫代碼。

基本上你錯了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);
}

編輯:對不起-第一次有一些錯誤-我現在對其進行了編輯,它應該像這樣工作。

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

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM