Линейное описание для разных размеров экрана

#android #android-linearlayout #screen-size

#Android #android-linearlayout #размер экрана

Вопрос:

У меня есть LinearLayout, который окружен ScrollView, чтобы сделать его прокручиваемым для небольших экранов. В линейном макете у меня есть 3 дочерних элемента, которые также являются linearlayout.

Это работает нормально. Но я также хочу поддерживать большие экраны. На данный момент я получаю больше места на экране, оставшегося в нижней части экрана. Я хочу, чтобы кнопки (панель навигации) всегда были внизу. для больших экранов (высота) Я хотел бы добавить в верхней части экрана пустое представление, которое заполняет пространство, которое больше предоставляется большим экраном.

Как я могу этого добиться? я много пробовал с атрибутом weight, но он не работал.

Здесь макет:

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="#8db9f6"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#8db9f6">
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_marginTop="8dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="test"
                android:id="@ id/test"
                android:paddingLeft="10dp"
                android:textStyle="bold"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <View
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"/>
            <ImageButton
                android:id="@ id/btnName"
                android:scaleType="center"
                android:layout_width="60dp"
                android:layout_height="48dp"
                android:src="@android:drawable/ic_menu_edit">
            </ImageButton>
        </LinearLayout>
        <EditText
            android:id="@ id/edittest"
            android:editable="false"
            android:hint="@string/test"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </EditText>
        <TextView
            android:text="@string/optionalText"
            android:id="@ id/test1"
            android:paddingLeft="10dp"
            android:textSize="12dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </TextView>
        <TextView
            android:id="@ id/space"
            android:layout_width="fill_parent"
            android:layout_marginTop="4dp"
            android:layout_height="3sp"
            android:background="#333142">
        </TextView>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_marginTop="5dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="TEST TESt"
                android:id="@ id/test2"
                android:paddingLeft="10dp"
                android:layout_marginTop="5dp"
                android:textStyle="bold"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <View
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"/>
            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:id="@ id/progressbar"/>
            <ImageButton
                android:id="@ id/btnPicture"
                android:scaleType="center"
                android:layout_width="60dp"
                android:layout_height="48dp"
                android:src="@android:drawable/ic_menu_edit">
            </ImageButton>
        </LinearLayout>
        <FrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:background="#ffffff"
            android:layout_gravity="center"
            android:layout_width="fill_parent"
            android:layout_height="150dip">
            <ImageView
                android:id="@ id/test3"
                android:layout_width="400dip"
                android:layout_height="150dip"
                android:layout_gravity="center"
                android:scaleType="fitCenter"
                android:src="@drawable/test1"/>
            <TextView
                android:id="@ id/text"
                android:layout_gravity="bottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="8dp"
                android:text="test test"/>
        </FrameLayout>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_gravity="bottom"
            android:layout_marginTop="3dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@ id/scaleMinus"
                android:textSize="14dp"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:text="@string/zoomOut"/>
            <Button
                android:id="@ id/scalePlus"
                android:textSize="14dp"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:layout_width="fill_parent"
                android:layout_height="40dp"
                android:text="@string/zoomIn"/>
        </LinearLayout>
        <TextView
            android:id="@ id/balken"
            android:layout_width="fill_parent"
            android:layout_marginTop="4dp"
            android:layout_height="3sp"
            android:background="#333142">
        </TextView>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_marginTop="5dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <TextView
                android:text="test test"
                android:id="@ id/test3"
                android:paddingLeft="10dp"
                android:textStyle="bold"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <View
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"/>
            <EditText
                android:id="@ id/test4"
                android:editable="false"
                android:layout_width="100dp"
                android:layout_height="wrap_content">
            </EditText>
            <TextView
                android:text="test"
                android:id="@ id/mtest4"
                android:paddingLeft="10dp"
                android:textStyle="bold"
                android:textSize="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>
            <View
                android:layout_width="wrap_content"
                android:layout_height="0dip"
                android:layout_weight="1"/>
            <ImageButton
                android:id="@ id/test5"
                android:scaleType="center"
                android:layout_width="60dp"
                android:layout_height="48dp"
                android:src="@android:drawable/ic_menu_edit">
            </ImageButton>
        </LinearLayout>
        <TextView
            android:id="@ id/balken"
            android:layout_width="fill_parent"
            android:layout_marginTop="4dp"
            android:layout_height="3sp"
            android:background="#333142">
        </TextView>
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:layout_gravity="bottom"
            android:background="#000000"
            android:paddingTop="5dp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@ id/save"
                android:textSize="18dp"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:layout_width="fill_parent"
                android:layout_height="60dp"
                android:text="@string/save"/>
            <Button
                android:id="@ id/cancel"
                android:textSize="18dp"
                android:layout_weight="1"
                android:layout_gravity="bottom"
                android:layout_width="fill_parent"
                android:layout_height="60dp"
                android:text="@string/abort"/>
        </LinearLayout>
    </LinearLayout>
</ScrollView>
  

Комментарии:

1. Вы пробовали использовать fill_parent в макетах?. Можете ли вы опубликовать layout.xml чтобы посмотреть, как это выглядит, чтобы мы могли вам лучше помочь?

2. да, я установил fill_parent . Я опубликую макет.

Ответ №1:

Оберните весь макет в относительный макет. Используйте относительный макет в качестве корневого элемента и установите для линейного макета, содержащего ваши кнопки, значение layout_alignParentBottom=»true», а для ScrollView — layout_alignParentTop=»true».

LinearLayout, содержащий кнопки и ScrollView, должен быть прямым дочерним элементом RelativeLayout.

 <RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
         <LinearLayout <-- containing the layout minus the buttons
               android:id="@ id/SV"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_alignParentTop="true">
            <The Rest Of The Layout>
         </LinearLayout>
         <LinearLayout
               android:id="@ id/LLButtons"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true">
          <Buttons>
          </LinearLayout>
</ScrollView>
  

Если вы хотите, чтобы кнопки всегда были в нижней части экрана, выровняйте parentbottom. Если вы хотите, чтобы кнопки всегда были внизу scrollview alignBelow=»@id / SV»

Комментарии:

1. Спасибо. Но начинается ли просмотр прокрутки?

2. хорошо, спасибо, это приятно. Но теперь пространство находится над кнопками. Я хочу, чтобы это было в верхней части экрана.

3. Вы хотите, чтобы макет был привязан к нижней части экрана, а любое пустое пространство вверху? Нет проблем. Buttons = alignParentBottom= true amp; LL с остальной частью макета будет alignAbove=»@id / LLButtons»

4. спасибо. У меня есть еще один вопрос. Можно ли увеличить LinearLayout, который содержит кнопки навигации, если у меня экран большего размера, что дает мне немного свободного места (которое у меня теперь есть в верхней части экрана)?

5. Вы будете присваивать параметр layout_weight линейным макетам верхнего уровня. Это заставит их расширяться, сжиматься в зависимости от веса, установленного для каждого.

Ответ №2:

android:layout_height="wrap_content" в вашем внутреннем LinearLayout ваша проблема здесь, изменение его на ‘fill_parent’ должно помочь. В общем, вы можете использовать hierarchyviewer для устранения подобных проблем с компоновкой.

Комментарии:

1. У меня есть 3 внутренних линейных описания. должен ли я использовать для всех из них fill_parent для высоты?