Кнопок ниже расширения scrollview
Макет, который я пытаюсь реализовать, (я думаю) довольно прост.
Один TextView расширяется в зависимости от того, что внутри. Ниже приведены две кнопки (ОК/Отмена).
Я могу заставить кнопки придерживаться нижней части экрана, и это работает отлично. Но я хотел бы иметь эти кнопки чуть ниже TextView.
Я пробовал много разных вещей, но вот мой последний макет:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView android:id="@+id/Scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
>
<TextView
android:id="@+id/Text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
<RelativeLayout android:id="@+id/buttons"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_below="@+id/Scroll"
android:layout_above="@+id/bottom"
>
<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<Button android:id="@+id/ButtonOK"
android:layout_height="80dp"
android:layout_width="fill_parent"
android:text="OK"
android:layout_weight="1"/>
<Button android:id="@+id/ButtonCancel"
android:layout_height="80dp"
android:layout_width="fill_parent"
android:text="Cancel"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout android:id="@+id/bottom"
android:layout_height="0px"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Пустая линейная выгрузка в конце является попыткой (неудачной), чтобы предотвратить прохождение просмотра в нижней части экрана...
Во всем, что я пробовал, либо кнопки находятся в нижней части экрана, либо они будут проходить мимо, когда TextView слишком большой.
Вот эскиз того, что я пытаюсь сделать: ![enter image description here]()
Ответы
Ответ 1
Цель состоит только в том, чтобы иметь прокручиваемую область, которая занимает всю страницу за вычетом суммы, занимаемой некоторыми кнопками внизу?
Если это случай, вы можете использовать LinearLayout
на внешнем уровне и использовать веса:
<LinearLayout
android:layout_height="wrap_content"
orientation="vertical"...>
<ScrollView
android:layout_height="0dp"
android:layout_weight="1"
.../>
<LayoutWithButtons
android:layout_height="wrap_content"
..../>
</LinearLayout>
Ответ 2
Это должен быть ответ в xml
android:fillViewport="true"
http://www.curious-creature.org/2010/08/15/scrollviews-handy-trick/
Ответ 3
Я нашел эту ссылку, которая действительно помогла мне.
http://www.droidnova.com/layout-technique-static-elements-below-scrollview,123.html
У меня есть некоторые специфические индивидуальные потребности для решения клавиатуры - я должен сам обрабатывать отображение и разрывать клавиатуру как элемент z-слоя в frameLayout. Я использую решение в этой ссылке для достижения этого и одновременно изменяю размер scrollView. Кнопки в режиме прокрутки перемещаются из нижней части экрана, чтобы скрыть верхнюю часть клавиатуры.
Ключ имеет андроид: fillViewport = "true" в scrollView. Затем программно устанавливая нижнее поле scrollView и верхнее поле нижнего элемента в -hight.
Вот мои два метода, которые его обрабатывают:
private void setMargins_noKeyboard()
{
ScrollView fieldsScroller = (ScrollView)findViewById(R.id.scroll_fields_view);
FrameLayout keyboardFrame = (FrameLayout)findViewById(R.id.keyboard_root);
int buttonsKeyboardSize = keyboardFrame.getHeight();
MarginLayoutParams frameParams = (MarginLayoutParams)keyboardFrame.getLayoutParams();
frameParams.setMargins(0, 0, 0, 0);
MarginLayoutParams scrollParams = (MarginLayoutParams)fieldsScroller.getLayoutParams();
scrollParams.setMargins(0, 0, 0, 0);
keyboardFrame.setLayoutParams(frameParams);
fieldsScroller.setLayoutParams(scrollParams);
}
private void setMargins_keyboard()
{
ScrollView fieldsScroller = (ScrollView)findViewById(R.id.scroll_fields_view);
FrameLayout keyboardFrame = (FrameLayout)findViewById(R.id.keyboard_root);
int buttonsKeyboardSize = keyboardFrame.getHeight();
MarginLayoutParams frameParams = (MarginLayoutParams)keyboardFrame.getLayoutParams();
frameParams.setMargins(0, -buttonsKeyboardSize, 0, 0);
MarginLayoutParams scrollParams = (MarginLayoutParams)fieldsScroller.getLayoutParams();
scrollParams.setMargins(0, 0, 0, buttonsKeyboardSize);
keyboardFrame.setLayoutParams(frameParams);
fieldsScroller.setLayoutParams(scrollParams);
}
Ответ 4
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:id="@+id/buttons"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
>
<LinearLayout
android:orientation="horizontal"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<Button android:id="@+id/ButtonOK"
android:layout_height="80dp"
android:layout_width="fill_parent"
android:text="OK"
android:layout_weight="1"/>
<Button android:id="@+id/ButtonCancel"
android:layout_height="80dp"
android:layout_width="fill_parent"
android:text="Cancel"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
<ScrollView android:id="@+id/ScrollParent"
android:layout_above="@id/buttons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView android:id="@+id/Scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
>
<TextView
android:id="@+id/Text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
</RelativeLayout>
Надеюсь, что это поможет
Ответ 5
Этот xml подходит для моего приложения:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/nameOfrecipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:layout_marginLeft="10sp"
android:layout_alignParentTop="true"
android:textSize="20sp"
android:text="Name of ingredients" />
<TableLayout
android:id="@+id/tableOfingredients"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/nameOfrecipe"
android:layout_marginTop="40sp"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp" >
</TableLayout>
<RelativeLayout android:id="@+id/l"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tableOfingredients"
>
<Button
android:id="@+id/addDirectionsB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Add..." />
<Button
android:id="@+id/cancelDirectionsB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/addDirectionsB"
android:text="Cancel" />
<ScrollView
android:id="@+id/directionsScroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/addDirectionsB">
<EditText
android:id="@+id/directionsET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</ScrollView>
</RelativeLayout>
</RelativeLayout>