2012-07-09 7 views
0

여기에서이 HorizontalScrollView (http://www.dev-smart.com/archives/34)를 사용하고 정적으로 xml을 사용하여 그림과 같이 세 개의 수평 레벨을 생성합니다.프로그래밍 방식으로 사용자 정의 레이아웃을 프로그래밍 방식으로 만들려면

<ScrollView 
    android:id="@+id/ScrollView01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:id="@+id/lineartoc1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/title1" 
      android:layout_width="wrap_content" 
      android:layout_height="30px" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      android:background="@drawable/tocheader" 
      android:gravity="center_vertical|center_horizontal" 
      android:minWidth="150px" 
      android:text="example" 
      android:textColor="#ffffff" 
      android:textSize="18dp" 
      android:textStyle="bold" /> 

     <a.b.c.d.HorizontalListView 
      android:id="@+id/listview" 
      android:layout_width="wrap_content" 
      android:layout_height="150dp" 
      android:layout_weight="1" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/title2" 
      android:layout_width="wrap_content" 
      android:layout_height="30dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      android:background="@drawable/tocheader" 
      android:gravity="center_vertical|center_horizontal" 
      android:minWidth="150dip" 
      android:text="example" 
      android:textColor="#ffffff" 
      android:textSize="18dp" 
      android:textStyle="bold" /> 

     <a.b.c.d.HorizontalListView 
      android:id="@+id/listview2" 
      android:layout_width="wrap_content" 
      android:layout_height="150dp" 
      android:layout_weight="3" 
      android:textStyle="bold" /> 


     <TextView 
      android:id="@+id/title3" 
      android:layout_width="wrap_content" 
      android:layout_height="30dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      android:background="@drawable/tocheader" 
      android:gravity="center_vertical|center_horizontal" 
      android:minWidth="150dip" 
      android:text="example" 
      android:textColor="#ffffff" 
      android:textSize="18dp" 
      android:textStyle="bold" /> 

     <a.b.c.d.HorizontalListView 
      android:id="@+id/listview3" 
      android:layout_width="wrap_content" 
      android:layout_height="150dp" 
      android:layout_weight="3" 
      android:textStyle="bold" /> 
     <!-- 
     <TextView 
      android:id="@+id/title4" 
      android:layout_width="300dp" 
      android:layout_height="30dp" 
      android:layout_marginTop="10dp" 
      android:layout_weight="1" 
      android:background="#88000000" 
      android:gravity="center_vertical|center_horizontal" 
      android:text="example" 
      android:textColor="#ffffff" 
      android:textSize="20dp" 
      android:textStyle="bold" /> 

     <a.b.c.d.HorizontalListView 
      android:id="@+id/listview4" 
      android:layout_width="wrap_content" 
      android:layout_height="300dp" 
      android:layout_weight="1" 
      android:textStyle="bold" /> 
     --> 
    </LinearLayout> 
</ScrollView> 

나는 각각 위에 텍스트가있는 3 개의 가로 목록보기를 만들었으며 이제는 상대적으로 다른 하나의 아래에 배치됩니다. 작동 방식은 처음에는 첫 번째 목록에만 일부 항목이 있습니다. 첫 번째 수평리스트 뷰에서 하나의 아이템을 클릭하면 다음 수평리스트가 생성되고 다음 레벨을 위해 계속된다. 하지만 xml에서 생성 된 이후로는 고정 된 수의 레벨 만 가질 수 있습니다. 제 경우에는 3 개입니다. 프로그래밍 방식으로 동일한 위치에서 똑같은 일을 어떻게 수행합니까? 'n'수준의 지원이 가능합니다. 또한 모든 것은 scrollview에 싸여있어 페이지가 끝나지 않고 n 레벨을위한 장소가 있습니다.

+0

'listview' 대신에'android-listview' 태그를 사용해야합니다.'listview'는 많은 것들 (예 : .NET'listview')에 적용될 수 있습니다 :) –

+0

글쎄, 실제로 넣고 싶지 않았습니다. 왜냐하면 내 코드는 안드로이드리스트 뷰를 포함하고 있지 않지만 사용자 정의 가로리스트 뷰는 일반 범용 뷰리스트를 포함하고 있기 때문에 우선적으로, 그러나 나는 당신의 요점을 본다. – Slartibartfast

답변

0

findViewById()를 사용하여 코드에서 LinearLayout에 대한 핸들을 얻은 다음 .addView (View child, LayoutParams)를 호출하고 그런 방법으로 하위 뷰를 설정할 수 있습니다.

LinearLayout 내부에서 뷰를 설정할 때는 LinearLayout.LayoutParams. *를 사용해야합니다.

관련 문제