Android:Проблемы с версткой XML
-
27-09-2019 - |
Вопрос
Я пытался создать RelativeLayout с заголовком, телом (списком) и нижним колонтитулом.Я хочу, чтобы заголовок был выше всего (выровнен по верху), тело (список) находилось под заголовком (при прокрутке), а нижний колонтитул (выровнен по низу) находился над телом (списком) - только когда он виден.
Мне удалось сделать это ИДЕАЛЬНО, когда дело доходит до того, как все выглядит, но, по-видимому, макет XML является причиной проблем, с которыми я сталкивался при нажатии на ListItems (смотрите здесь, и здесь).
Чтобы решить проблемы, мне пришлось обернуть ListView с помощью LinearLayout и поместить его между верхним и нижним колонтитулами (в XML).Это решает проблемы с щелчком ListItems, НО создает новую проблему - нижний колонтитул скрывает последний элемент ListView, хотя список действительно находится под заголовком при прокрутке его вниз.
Я здесь в полном отчаянии :\
Это XML, который создает идеальный макет, но вызывает проблемы с щелчком по элементу списка:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Droidmarks"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add_bookmark"
android:textSize="20dip"
android:textColor="#a0cb26"
android:shadowColor="#7a9b1b"
android:shadowRadius="5"
android:layout_marginLeft="5dip"
android:gravity="left|center" />
<Button
android:id="@+id/add_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/top_background"
android:visibility="invisible"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</LinearLayout>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/top_control_bar"
android:layout_above="@id/bottom_control_bar"
android:drawSelectorOnTop="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:cacheColorHint="#ffffff"
android:listSelector="@drawable/selected_item"
/>
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Bookmarks found. You can add one by clicking the star."
android:layout_below="@id/top_control_bar" android:layout_above="@id/bottom_control_bar" />
</RelativeLayout>
Это XML, который скрывает последний элемент списка, но решает проблемы с щелчками:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
android:layout_alignParentTop="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Droidmarks"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/add_bookmark"
android:textSize="20dip"
android:textColor="#a0cb26"
android:shadowColor="#7a9b1b"
android:shadowRadius="5"
android:layout_marginLeft="5dip"
android:gravity="left|center" />
<Button
android:id="@+id/add_bookmark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</RelativeLayout>
<LinearLayout
android:id="@+id/listArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@id/top_control_bar"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:cacheColorHint="#ffffff" />
<TextView android:id="@android:id/empty" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No Bookmarks found. You can add one by clicking the star." />
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_above="@id/listArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/top_background"
android:orientation="horizontal"
android:visibility="visible"
>
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:layout_alignParentRight="true"
android:text="Add"
/>
</LinearLayout>
</RelativeLayout>
Я был бы признателен за любую помощь, чтобы как-то найти решение для обеих проблем!
Спасибо!
Решение
Могу я спросить, почему вы вообще используете RelativeLayout для контейнера?Кажется, это идеальное место для простого использования LinearLayout с взвешенной центральной областью (listview), которая настроена так, чтобы занимать всю оставшуюся область после верхнего и нижнего колонтитулов wrap_height-set.RelativeLayouts отлично подходят для упрощения глубоко вложенных макетов, которые содержат как горизонтальные, так и вертикальные элементы, но ваш внешний слой здесь в любом случае содержит только 3 вертикальных элемента, поэтому используйте LinearLayout с orientation:vertical
если только я здесь чего-то не упускаю.
Редактировать:Короче говоря, попробуйте это;это должно дать вам верхний колонтитул фиксированной высоты, нижний колонтитул завершающей высоты и центральную область, занимающую все оставшееся пространство.Убедитесь, что внешний макет установлен на fill_parent
высота и средняя область установлены на фиксированную высоту, но с layout_weight="1"
.Мне нравится использовать "0px" для базовой layout_height, когда я использую layout_weight, просто чтобы мне было ясно, когда я вернусь и перечитаю это позже.
<?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"
android:background="#ffffff"
>
<RelativeLayout
android:background="@drawable/top_background"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:id="@+id/top_control_bar"
> <!-- snip - inner contents unchanged -->
</RelativeLayout>
<LinearLayout
android:id="@+id/listArea"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"
android:orientation="horizontal"
><!-- snip - inner contents unchanged -->
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_control_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_background"
android:orientation="horizontal"
android:visibility="visible"
><!-- snip - inner contents unchanged -->
</LinearLayout>
</LinearLayout>