2013-12-08 3 views
0

Я действительно разочарованы с макетом, как показано ниже:Android Layout с ListView

enter image description here

Я хотел бы иметь ListView и ниже, что мне нужно иметь две кнопки бок о бок, как показано выше (LIstview должен быть прокручиваемым, а кнопки должны быть в нижней панели, т. Е. ... в списке списка). Я пробовал использовать android:layout_above и android:layout_below, но ничего не получилось. Кто-нибудь может подумать о том, как достичь этого макета?

Это то, что мой код макета сейчас на самом деле у меня есть этот код, где я могу достичь этого, но я не могу нажать на кнопки здесь.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/secondLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="15dp" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/firstLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/secondLayout" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:background="#00ffffff" 
    android:gravity="bottom" 
    android:orientation="horizontal" 
    android:padding="15dp" > 

    <Button 
     android:id="@+id/btnPrevious" 
     android:layout_width="wrap_content" 
     android:layout_height="35dp" 
     android:layout_marginLeft="10dp" 
     android:layout_weight="1" 
     android:background="@drawable/redbutton" 
     android:onClick="" 
     android:text="Previous" 
     android:textColor="#ffffff" /> 

    <Button 
     android:id="@+id/btnNext" 
     android:layout_width="wrap_content" 
     android:layout_height="35dp" 
     android:layout_marginLeft="10dp" 
     android:layout_weight="1" 
     android:background="@drawable/redbutton" 
     android:text="Next" 
     android:textColor="#ffffff" /> 
</LinearLayout> 

+0

он работает на устройстве. даже щелчок работает. какую версию для Android вы используете. Я тестировал 4,4 и 2,3. – rachit

ответ

0

Шаг # 1: Избавиться от бесполезного LinearLayout упаковки вашего ListView, перемещая атрибут android:padding в ListView.

Шаг № 2: Добавить android:layout_alignParentTop="true" и android:layout_above="@+id/firstLayout в ListView.

Шаг № 3: Избавьтесь от android:layout_above="@id/secondLayout" от оставшихся LinearLayout, так как вы определенно не хотите, чтобы над ListView.

Шаг № 4: Переключитесь с dp на sp на ваши размеры Button, чтобы учесть масштабирование шрифтов.

Шаг 5: Либо измените ширину LinearLayout в wrap_content или удалить android:layout_centerHorizontal="true", как вы не можете центрировать то, что заполняет все доступное пространство.

С манжетой, остальное должно быть в порядке или, по крайней мере, быть намного ближе к тому, что вы хотите.

0

Как насчет добавления кнопок в панель действий?

В противном случае обернуть ListView внутри ScrollView и сделать свой RelativeLayout в LinearLayout

+0

Не помещайте 'ListView' в' ScrollView'. Даже если бы это сработало, это не решило бы проблему ОП. – CommonsWare

+0

ОК, спасибо за исправление меня :) – bbuecherl

0

Это может работать для вас:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_above="@+id/firstLayout" /> 

    <LinearLayout 
     android:id="@+id/firstLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:background="#00ffffff" 
     android:orientation="horizontal" 
     android:padding="15dp" > 

     <Button 
      android:id="@+id/btnPrevious" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:layout_marginLeft="10dp" 
      android:layout_weight="1" 
      android:onClick="" 
      android:text="Previous" 
      android:textColor="#ffffff" /> 

     <Button 
      android:id="@+id/btnNext" 
      android:layout_width="wrap_content" 
      android:layout_height="35dp" 
      android:layout_marginLeft="10dp" 
      android:layout_weight="1" 
      android:text="Next" 
      android:textColor="#ffffff" /> 
    </LinearLayout> 

</RelativeLayout> 
0

это работает. С какими проблемами вы сталкиваетесь?

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

<LinearLayout 
    android:id="@+id/secondLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="15dp" > 

    <ListView 
     android:id="@+id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

<LinearLayout 
    android:id="@+id/firstLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/secondLayout" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:background="#00ffff" 
    android:gravity="bottom" 
    android:orientation="horizontal" 
    android:padding="15dp" > 

    <Button 
     android:id="@+id/btnPrevious" 
     android:layout_width="wrap_content" 
     android:layout_height="35dp" 
     android:layout_marginLeft="10dp" 
     android:layout_weight="1" 
     android:background="#FF0000" 
     android:onClick="test" 
     android:text="Previous" 
     android:textColor="#ffffff" /> 

    <Button 
     android:id="@+id/btnNext" 
     android:layout_width="wrap_content" 
     android:layout_height="35dp" 
     android:layout_marginLeft="10dp" 
     android:layout_weight="1" 
     android:onClick="test" 
     android:background="#FF0000" 
     android:text="Next" 
     android:textColor="#ffffff" /> 
</LinearLayout> 

+0

@ rachit-Я не могу нажать кнопки и перейти к макету, если без списка и с помощью простых кнопок я могу их кликнуть, но когда я добавляю listview в макет, список при загрузке из внешнего класса dosent делает мою кнопку интерактивной и проверенной в 4.4 – coder

+0

@coder, не знаю человека. Я не могу восстановить проблему. похоже, работает на моем устройстве. щелкните также работу. : O :( – rachit