2013-10-04 3 views
0

그래서 조합을 시도하려고합니다. 보기이 설정되었습니다. 그들은 끊임없이 수직 목록 Textviews 서로 그 아래에 다음 수평 상단 버튼EDITTEXT 상자가 있어야합니다. 수직리스트는 사용자 (이 일어나는 동안 계속 표시한다 상단의 버튼과 글고 )를 TextViews 통해 아래로 스크롤 할 수있게하는 있는 ScrollView 묶어야한다.스크롤보기 내에서 수평 및 수직 선형 레이아웃

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

에 오신 것을 환영합니다. 코드에 어떤 문제가 있습니까? 당신이 얻는 결과는 무엇이며, 의도 된 결과와 다른 점은 무엇입니까? 감사합니다. – dic19

+0

. 코드는 편집 텍스트를 표시하고 있으며 버튼과 편집 텍스트는 표시되지 않습니다. 의도 한 결과는 edittext와 버튼이 상단에 있고 사용자가 textviews를 스크롤 할 때 거기에 머물러 있다는 것입니다. – 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에서 처리하고 그런 식으로 코드를 작성하는 것이 좋습니다.

setContentView(R.layout.my_xml_layout); 

이 방법 - 당신을 일반적으로 : 다음 다음과 같이 단 한 번으로 설정 -

+0

어떻게 수평 그룹이 항상 상단에 있고 수직 그룹이 아래에 있도록보기 그룹을 설정할 수 있습니까? 가로 레이아웃이지만 스크롤 할 수 있습니까? – algorhythm

0

은 기본적으로 당신은 잘못 된 setContentView ...
내가 강하게 XML로 레이아웃을 정의하는 제안이있어 귀하의 레이아웃을 볼 수있는 가능성이 있으며, 레이아웃을 정의하는 것이 더 쉽습니다 (특히, 언젠가 이것을 변경하기로 결정하는 경우). 죄송합니다 - 몇 가지 오류가 있었다 :

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

편집 -

당신은 그러나 코드에서 할 wan't 경우는, 당신이해야 할 것이다 것은 (하지만 작동합니다 검증되지 않은) 다음과 같은 뭔가 처음으로 - 지금 편집 했으니 까.

+0

도움을 주셔서 감사합니다. 당신의 충고는 내가 원했던 뷰 (상단의 버튼과 edittext)를 얻는 것을 도왔지만 스크롤 뷰는 나에게 수직 레이아웃의 textviews를 스크롤 할 수 없도록했다. 나는 아마 sccrollview와 수직 레이아웃을 서로 추가하는 것으로 보이는 편집 된 코드의 15 번과 17 번 라인에 문제가 있다고 생각한다. – algorhythm

+0

아하는 조언을 더 이상 제공 할 수 있다면 괜찮지 만 내가 가진 문제는 가로 레이아웃이 textviews를 스크롤 할 때 화면 밖으로 스크롤된다는 것입니다. 가로 레이아웃을 스크롤하면서 화면 상단에 수평 레이아웃을 유지할 수 있습니까? – algorhythm

관련 문제