2016-06-22 3 views
0

안드로이드 스튜디오를 사용하여 앱을 개발하는 중, 위치에 따라 변경되는 옵션 목록이있는 테이블이 필요합니다. 예를 들어 뉴욕에있을 때 표시하고 싶습니다. 3 가지 옵션이 있지만 샌디에고에 있다면 5 가지가 필요합니다. 제 생각에는 wrap_content가 다른 옵션과 함께 다른 선형 레이아웃과 함께 높이가있는 선형 레이아웃을 사용하는 것이 었습니다. 문제는 내부 레이아웃을 수정 (추가/제거)하는 방법이나이 아이디어를 구현할 수있는 더 좋은 방법이 있는지 모르겠다는 것입니다. 또한 사용자는 옵션 중 하나를 선택할 수 있으며 클릭 할 수 있습니다. 여기 안드로이드 스튜디오의 위치에 따라 동적 크기 조정 선형 레이아웃

이는 RecyclerView 더 잘 수행 될 수있다

enter image description here enter image description here

답변

1

선형 레이아웃을 동적으로 추가하려고 시도한 코드는 다음과 같습니다. :) 먼저 선형 레이아웃을 동적으로 만들려는 부모 레이아웃을로드합니다. 그런 다음 표시 할 레이아웃 수를 결정하고 코드의 'NumberOfLayouts'변수에 전달하면 선형 레이아웃이 동적으로 만들어집니다.

/*Loading the Parent Layout*/ 
LinearLayout ParentLayout = (LinearLayout)findViewById(R.id.ParentLayout); 

    /*Dynamically creating your linear layouts*/ 
    for(int i=0; i<NumberOfLayouts; i++){ 
     LinearLayout linearLayout = new LinearLayout(getApplication()); 

     TextView textView = new TextView(this); 
     textView.setText("Sample Text"); 

     linearLayout.addView(textView); 

     ParentLayout.addView(linearLayout); 

     /*Adding listener for the individual layouts*/ 
     linearLayout.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       //Your code or method to be executed while clicking the dynamically created linear layout 
      } 
     }); 
    } 
1
 // first, find the container view by using findViewById; 
    LinearLayout root = (LinearLayout) findViewById(R.id.container); 

    // then inflate the child view by using an inflater 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View childView = inflater.inflate(R.layout.childView, null); 

    // finally, add the child view to the container 
    root.addView(childView); 
2

몇 가지 예입니다. 뷰의 목록을 생성하고 나타나는 수 및 디자인을 제어 할 수 있습니다. RecyclerView는 사용자가 가지고있는 레이아웃에 맞게 스크롤 할 수 있습니다.