2010-10-21 17 views
12

Próbuję utworzyć układ, w którym jest widok tekstowy u góry ekranu i dolny pasek u dołu ekranu. Każdy z tych widoków powinien pozostać na miejscu, a pomiędzy 2 powinien być ListView.Układ Android z ListView między "górnym paskiem" i "dolnym paskiem"

TOPBAR 
LISTVIEW (scrollable) 
BOTTOM BAR 

mój układ (patrz niżej) prawie prace: górne i dolne elementy pozostają stałe i zwoje listview. "Problemem" jest to, że ostatni wiersz mojego ListView pozostaje ukryty za dolnym paskiem.

Jakieś sugestie dotyczące dopasowania układu?

Dzięki!

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android"> 
    <LinearLayout 
      android:layout_alignParentTop="true" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" 
      > 
     <TextView 
       android:background="@drawable/blackgrad" 
       android:gravity="center" 
       android:id="@+id/title" 
       android:layout_height="50dip" 
       android:layout_width="fill_parent" 
       android:text="blah blah" 
       android:textColor="#ffffff" 
       android:textSize="18sp" 
       /> 
     <ListView 
       android:background="#ffffff" 
       android:cacheColorHint="#ffffffff" 
       android:id="@android:id/list" 
       android:layout_height="fill_parent" 
       android:layout_width="fill_parent" 
       /> 
    </LinearLayout> 
    <LinearLayout 
      android:background="#efefef" 
      android:layout_alignParentBottom="true" 
      android:layout_centerHorizontal="true" 
      android:layout_width="fill_parent" 
      android:layout_height="50dip" 
      android:orientation="horizontal"> 
     <Button 
       android:layout_height="wrap_content" 
       android:id="@+id/back" 
       android:text="Back" 
       android:layout_width="wrap_content" /> 

     <Button 
       android:layout_height="wrap_content" 
       android:id="@+id/home" 
       android:text="Home" 
       android:layout_width="wrap_content" /> 

     <Button 
       android:layout_height="wrap_content" 
       android:id="@+id/next" 
       android:text="Next" 
       android:layout_width="wrap_content" /> 


    </LinearLayout> 
</RelativeLayout> 

Odpowiedz

27

Oto prosta sugestia. Zrób nowy XML i gotowe.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <TextView 
     android:text="Header" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"/> 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
    <TextView 
     android:text="Footer" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom"/> 
</LinearLayout> 
+0

To nie zadziała. Dolny widok tekstowy będzie obejmował część dolnej części listview – Falmarri

+0

@Falmarri, w moich rękach stopka tego układu nie zasłania w ogóle widoku listy. – user141146

+0

@Falmarri, próbuję go teraz i działa dokładnie tak, jak powinien. Wydaje się, że działa także dla user141146. – glr

4

Osiągnąłem to, co próbujesz zrobić w podobny sposób, używając identyfikatorów i atrybutów layout_below/layout_above. View s są ułożone w inny sposób, ale wynik końcowy jest taki sam. A ListView między dwoma innymi LinearLayout s, jeden na górze i jeden na dole.

W ListView widać mam:

android:layout_above="@+id/event_list_buttons" 
android:layout_below="@id/title_container" 

Ci stanowisko atrybuty ListView między moim górnej i dolnej LinearLayout s.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout style="@style/TitleBar"> 
     <ImageView style="@style/TitleBarLogo" android:src="@drawable/logo_small_transparent" /> 

     <View style="@style/TitleBarSpring" /> 

     <ImageView style="@style/TitleBarSeparator" /> 
     <ImageButton style="@style/TitleBarAction" android:src="@drawable/ic_title_refresh" 
      android:id="@+id/title_refresh_button" android:onClick="onClick" /> 
     <ProgressBar style="@style/TitleBarProgressIndicator" 
      android:id="@+id/title_refresh_progress" android:visibility="gone" /> 

     <ImageView style="@style/TitleBarSeparator" /> 
     <ImageButton style="@style/TitleBarAction" android:src="@drawable/ic_title_search" /> 
    </LinearLayout> 

    <ListView android:layout_width="fill_parent" 
     android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/android:list" android:layout_above="@+id/event_list_buttons" 
     android:layout_below="@id/title_container" android:fastScrollEnabled="true" 
     android:background="@color/white"></ListView> 

    <LinearLayout android:layout_width="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/event_list_buttons" 
     android:layout_height="wrap_content" android:layout_alignParentBottom="true" 
     android:gravity="center_horizontal"> 

     <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:id="@+id/events_list_view_organizing_button" 
      android:text="Organizing" android:onClick="onClick"></Button> 
     <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:id="@+id/events_list_view_attending_button" 
      android:text="Attending" android:onClick="onClick"></Button> 

    </LinearLayout> 
</RelativeLayout> 
5

Problem z układem OPS jest to, że pierwszy układ liniowy jest ustawiony jako android:layout_height="fill_parent" który spowoduje, że pierwszy układ liniowy wypełnić rodzica. Nie wie, że zamierzasz dodać kolejne widoki. Co musisz zrobić, to dodać widoki w kolejności, w jakiej znają swój rozmiar. Więc znasz rozmiar górnej pary i dolny pasek, możesz dodać je jako android:layout_height="wrap_content". Następnie dodaj listview, ponieważ jego rozmiar jest nieznany podczas kompilacji.

+0

Dzięki. to było bardzo pomocne! – user141146

2

Znalazłem inny sposób, aby to zrobić z layout_weight, gdy są ujęte w układ liniowy. Jeśli ustawisz wagę ListView na coś arbitralnie dużego, a ciężar statycznego paska dolnego na coś bardzo małego, efekt zadziała. Zarówno widok listy, jak i wysokość dolnego paska muszą być ustawione na "wrap_content", aby wszystko działało razem.

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:color="@color/bg" 
xmlns:android="http://schemas.android.com/apk/res/android" > 

    <ListView 
     android:id="@android:id/list" 
     android:padding="5dp" 
     android:divider="@color/bg" 
     android:dividerHeight="4dp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="20" 
     > 
    </ListView> 

    <TextView android:text="Sorry, no results were found" android:id="@android:id/empty" android:layout_width="fill_parent" android:layout_height="fill_parent"></TextView> 


    <include layout="@layout/bottom_bar" android:layout_weight=".1" android:id="@+id/bottomBar" android:layout_alignParentBottom="true" android:layout_width="fill_parent" android:layout_height="wrap_content"></include> 

1

Znalazłem myślę, że prostsze rozwiązanie tylko od this postu

Wystarczy zadeklarować zarówno bary PRZED viewlist dodanie android: layout_alignParentBottom = "true" i Android: layout_alignParentTop = "true " w każdej sprawie.

Następnie określ w układzie listy widoków, który ustawia między nimi android: layout_above = "id_of_footer" i android: layout_below = "id_of_header" właściwości.

To działa przykład z mojej aplikacji. Po prostu z paskiem stopki.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/bottom_bar" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:background="#333333" 
    android:padding="@dimen/padding_small" > 

    <Button 
     android:id="@+id/bt_add_session" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="addSession" 
     android:text="@string/add_session" /> 

    <TextView 
     android:id="@+id/lb_summary" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="right" 
     android:text="@string/summary_example" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:textColor="#FFFFFF" /> 
</LinearLayout> 

<ListView 
    android:id="@+id/vl_sessions" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/bottom_bar" > 
</ListView> 

3

miałem tego rodzaju problemu. Że stosuje się trzy układy, a nie z dwóch, a tego rodzaju rozwiązania:

  • 3 układy są przechowywane w RelativeLayout jak w powyższych rozwiązań
  • układ zawierający listview jest związana z „Topbar” (firstLayout) przy użyciu android: layout_below
  • trzeci układ staje się "bottombar" za pomocą android: layout_alignParentBottom = "true"
  • i najważniejszą rzeczą (problem): dodam android: layout_marginBottom do układu zawierającego ListView. Musiałem wprowadzić kilka poprawek do wartości, zanim znajdę wystarczający margines.

    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/firstLayout" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:gravity="left" 
        android:orientation="horizontal" > 
    
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:layout_gravity="center_vertical" 
         android:layout_marginTop="10dip" 
         android:text="@string/show_history_text" /> 
    </LinearLayout> 
    
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/secondLayout" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@id/firstLayout" 
        android:layout_marginBottom="50dip" > 
    
        <ListView 
         android:id="@+id/ListNotes" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" /> 
    </LinearLayout> 
    
    <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/thirdLayout" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:background="@color/LightSteelBlue" 
        android:gravity="left" 
        android:orientation="horizontal" > 
    
        <Button 
         android:id="@+id/btnClose" 
         android:layout_width="fill_parent" 
         android:layout_height="wrap_content" 
         android:layout_gravity="bottom" 
         android:gravity="center_vertical|left" 
         android:onClick="BtnCloseClick" 
         android:text="@string/btn_close_history_text" /> 
    </LinearLayout> 
    

Powiązane problemy