Android - помощь, необходимая для устранения несоответствия макета (RelativeLayout и LinearLayout) - PullRequest
0 голосов
/ 01 августа 2011

Я создаю приложение с многоразовой цифровой клавиатурой. Клавиатура была помещена в собственный XML-файл, поэтому ее можно включать в любое место в приложении. Я хочу разделить экран для текущего действия на две части: цифровую клавиатуру фиксированного размера внизу и RelativeLayout (RL) выше с остальными элементами управления.

Проблема в том, что RL не ведет себя сам. В приведенном ниже примере RL занимает весь экран, а клавиатура не отображается. Действительно странно то, что когда я меняю месторасположение, размещаю включение с клавиатурой вверху, экран отображается, как и ожидалось, с клавиатурой и RL, каждый занимает примерно половину.

Другими словами: когда клавиатура находится сверху, все в порядке (но клавиатура не в том месте); но когда я включаю клавиатуру внизу, клавиатура не отображается, и RL занимает весь экран.

Помощь! Такое несоответствие сводит меня с ума, и я на грани того, чтобы швырнуть компьютер через всю комнату.

XML-код действия:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/statsTabLayout"
    android:scaleType="fitXY"
    android:background="#ffffff" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:baselineAligned="true" 
    android:orientation="vertical">

    <RelativeLayout android:id="@+id/statsTopLayout"
        android:background="#ffffff" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:orientation="vertical">


    <TextView android:id="@+id/textViewTopMargin" 
        android:text="                              " 
        android:textColor="#FFFFFF"
        android:textSize = "5sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_gravity="top"
        android:layout_alignParentLeft="true"
        android:paddingTop="0px"
    ></TextView>

    <TextView android:id="@+id/textViewAverage" 
        android:text="Average =    (%)" 
        android:textColor="#000000"
        android:textSize = "25sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewTopMargin"
        android:layout_alignParentLeft="true"
    ></TextView>

    <Button android:id="@+id/buttonReset" 
        android:text="reset" 
        android:onClick="resetButtonClick" 
        android:textSize = "12sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/textViewAverage" 
        android:layout_alignBottom="@+id/textViewAverage"
        android:layout_below="@+id/textViewTopMargin"
        android:layout_alignParentRight="true"
    ></Button>

    <TextView android:id="@+id/textViewHi" 
        android:text="High= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_alignParentLeft="true"
    ></TextView>

    <TextView android:id="@+id/textViewLow" 
        android:text="Low= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_centerHorizontal="true" 
    ></TextView>

    <TextView android:id="@+id/textViewMax" 
        android:text="Max= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_alignParentRight="true"
    ></TextView>

    <TextView android:id="@+id/textViewScore" 
        android:text="Score = " 
        android:textColor="#000000" 
        android:textSize = "30sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_above="@+id/textViewScoreCalculation"
        android:layout_alignParentLeft="true"
    ></TextView>

    <TextView android:id="@+id/textViewScoreCalculation" 
        android:text="(score calculation)" 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:textStyle="italic"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="10px"
        android:layout_above="@+id/editTextPoints"
        android:layout_alignParentRight="true"
    ></TextView>

    <TextView android:id="@+id/textViewPoints" 
        android:text="Enter Points: " 
        android:textSize = "30sp"
        android:textColor="#000000"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_above="@+id/textViewBottomMargin"
        android:layout_alignParentLeft="true"
        android:paddingBottom="10px"
    ></TextView>

    <EditText android:id="@+id/editTextPoints" 
        android:text="" 
        android:digits="-0123456789."
        android:windowSoftInputMode="stateVisible"
        android:focusable="true"
        android:textSize = "30sp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/textViewPoints" 
        android:layout_alignBottom="@+id/textViewPoints"
        android:layout_above="@+id/textViewBottomMargin"
        android:layout_toRightOf="@+id/textViewPoints" 
        android:paddingBottom="10px"
    ></EditText>

    <TextView android:id="@+id/textViewBottomMargin" 
        android:text="                              " 
        android:textColor="#FFFFFF"
        android:textSize = "5sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true" 
        android:paddingBottom="0px"
    ></TextView>


    </RelativeLayout>


    <include layout="@layout/numerickeyboard" />

</LinearLayout>

Родительская раскладка для прилагаемой клавиатуры:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="104pt" 
    android:id="@+id/numberPadLayoutContainer" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent"
    android:background="#404040"
    android:layout_alignParentBottom="true" 
>

Есть предложения?

Ответы [ 2 ]

0 голосов
/ 01 августа 2011

Проблема заключается в том, что некоторые атрибуты дочернего макета для RelativeLayout, такие как layout_alignParentBottom="true", требуют, чтобы макет самостоятельно определил размеры, прежде чем размещать дочерний элемент, который хочет выровнять по его низу. Фактически, когда дочерний элемент запрашивает alignParentBottom, он заставляет RelativeLayout использовать все доступное ему вертикальное пространство.

Последние несколько элементов в вашем макете располагаются над последним элементом, TextView android:id="@+id/textViewBottomMargin", который определяется как alignParentBottom. Я исправил макет, изменив последние несколько элементов так, чтобы они выравнивались под предыдущими элементами, а не над последним элементом.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/statsTabLayout"
    android:scaleType="fitXY"
    android:background="#ffffff" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    android:baselineAligned="true" 
    android:orientation="vertical">

    <RelativeLayout android:id="@+id/statsTopLayout"
        android:background="#ffffff" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:orientation="vertical">


    <TextView android:id="@+id/textViewTopMargin" 
        android:text="                              " 
        android:textColor="#FFFFFF"
        android:textSize = "5sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_gravity="top"
        android:layout_alignParentLeft="true"
        android:paddingTop="0px"
    ></TextView>

    <TextView android:id="@+id/textViewAverage" 
        android:text="Average =    (%)" 
        android:textColor="#000000"
        android:textSize = "25sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewTopMargin"
        android:layout_alignParentLeft="true"
    ></TextView>

    <Button android:id="@+id/buttonReset" 
        android:text="reset" 
        android:onClick="resetButtonClick" 
        android:textSize = "12sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/textViewAverage" 
        android:layout_alignBottom="@+id/textViewAverage"
        android:layout_below="@+id/textViewTopMargin"
        android:layout_alignParentRight="true"
    ></Button>

    <TextView android:id="@+id/textViewHi" 
        android:text="High= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_alignParentLeft="true"
    ></TextView>

    <TextView android:id="@+id/textViewLow" 
        android:text="Low= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_centerHorizontal="true" 
    ></TextView>

    <TextView android:id="@+id/textViewMax" 
        android:text="Max= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_alignParentRight="true"
    ></TextView>

    <TextView android:id="@+id/textViewScore" 
        android:text="Score = " 
        android:textColor="#000000" 
        android:textSize = "30sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewHi"
        android:layout_alignParentLeft="true"
    ></TextView>

    <TextView android:id="@+id/textViewScoreCalculation" 
        android:text="(score calculation)" 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:textStyle="italic"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="10px"
        android:layout_below="@+id/textViewScore"
        android:layout_alignParentRight="true"
    ></TextView>

    <TextView android:id="@+id/textViewPoints" 
        android:text="Enter Points: " 
        android:textSize = "30sp"
        android:textColor="#000000"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewScoreCalculation"
        android:layout_alignParentLeft="true"
        android:paddingBottom="10px"
    ></TextView>

    <EditText android:id="@+id/editTextPoints" 
        android:text="" 
        android:digits="-0123456789."
        android:windowSoftInputMode="stateVisible"
        android:focusable="true"
        android:textSize = "30sp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/textViewPoints" 
        android:layout_alignBottom="@+id/textViewPoints"
        android:layout_toRightOf="@+id/textViewPoints" 
        android:paddingBottom="10px"
    ></EditText>

    <TextView android:id="@+id/textViewBottomMargin" 
        android:text="                              " 
        android:textColor="#FFFFFF"
        android:textSize = "5sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/editTextPoints"
        android:paddingBottom="0px"
    ></TextView>


    </RelativeLayout>


    <include layout="@layout/numerickeyboard" />

</LinearLayout>

Я также изменил клавиатуру, удалив оператор android:layout_alignParentBottom="true".

Это снимок экрана с эмулятором HVGA_1.6 (для скриншота я выделил область клавиатуры зеленым).

enter image description here

В соответствии с измененным вопросом, "также хочу, чтобы statsTopLayout заполнял экран, когда мы не включаем клавиатуру" (см. Комментарии) , я скромно отправляю эти изменения в исходную раскладку основной и клавиатуры файлы:

В исходном main.xml измените корень LinearLayout на RelativeLayout, затем переместите <include> в качестве первого элемента. В этот перемещенный элемент <include> добавьте оператор android:id="@+id/numberPadLayoutContainer" из numerickeyboard.xml (и удалите его из numerickeyboard.xml (main.xml не будет компилироваться без объявления локального идентификатора). Третий элемент теперь является вложенным RelativeLayout, который теперь определяет layout_height="fill_parent" и layout_above="@id/numberPadLayoutContainer".

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/statsTabLayout"
    android:background="#ffffff" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" 
    >

    <include layout="@layout/numerickeyboard" 
            android:id="@+id/numberPadLayoutContainer" 
        />

    <RelativeLayout android:id="@+id/statsTopLayout"
        android:background="#ffffff" 
        android:layout_height="fill_parent" 
        android:layout_width="wrap_content" 
        android:layout_above="@id/numberPadLayoutContainer">


    <TextView android:id="@+id/textViewTopMargin" 
        android:text="                              " 
        android:textColor="#FFFFFF"
        android:textSize = "5sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_gravity="top"
        android:layout_alignParentLeft="true"
        android:paddingTop="0px"
    ></TextView>

    <TextView android:id="@+id/textViewAverage" 
        android:text="Average =    (%)" 
        android:textColor="#000000"
        android:textSize = "25sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewTopMargin"
        android:layout_alignParentLeft="true"
    ></TextView>

    <Button android:id="@+id/buttonReset" 
        android:text="reset" 
        android:onClick="resetButtonClick" 
        android:textSize = "12sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/textViewAverage" 
        android:layout_alignBottom="@+id/textViewAverage"
        android:layout_below="@+id/textViewTopMargin"
        android:layout_alignParentRight="true"
    ></Button>

    <TextView android:id="@+id/textViewHi" 
        android:text="High= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_alignParentLeft="true"
    ></TextView>

    <TextView android:id="@+id/textViewLow" 
        android:text="Low= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_centerHorizontal="true" 
    ></TextView>

    <TextView android:id="@+id/textViewMax" 
        android:text="Max= " 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_below="@+id/textViewAverage"
        android:layout_alignParentRight="true"
    ></TextView>

    <TextView android:id="@+id/textViewScore" 
        android:text="Score = " 
        android:textColor="#000000" 
        android:textSize = "30sp"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="5px"
        android:layout_above="@+id/textViewScoreCalculation"
        android:layout_alignParentLeft="true"
    ></TextView>

    <TextView android:id="@+id/textViewScoreCalculation" 
        android:text="(score calculation)" 
        android:textColor="#000000"
        android:textSize = "20sp"
        android:textStyle="italic"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:paddingBottom="10px"
        android:layout_above="@+id/editTextPoints"
        android:layout_alignParentRight="true"
    ></TextView>

    <TextView android:id="@+id/textViewPoints" 
        android:text="Enter Points: " 
        android:textSize = "30sp"
        android:textColor="#000000"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_above="@+id/textViewBottomMargin"
        android:layout_alignParentLeft="true"
        android:paddingBottom="10px"
    ></TextView>

    <EditText android:id="@+id/editTextPoints" 
        android:text="" 
        android:digits="-0123456789."
        android:windowSoftInputMode="stateVisible"
        android:focusable="true"
        android:textSize = "30sp"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignTop="@+id/textViewPoints" 
        android:layout_alignBottom="@+id/textViewPoints"
        android:layout_above="@+id/textViewBottomMargin"
        android:layout_toRightOf="@+id/textViewPoints" 
        android:paddingBottom="10px"
    ></EditText>

    <TextView android:id="@+id/textViewBottomMargin" 
        android:text="                              " 
        android:textColor="#FFFFFF"
        android:textSize = "5sp"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true" 
        android:paddingBottom="0px"
    ></TextView>
    </RelativeLayout>
</RelativeLayout>

Снимок экрана с оператором <include>:

enter image description here

Снимок экрана без оператора <include> и без оператора android:layout_above="@id/numberPadLayoutContainer":

enter image description here

0 голосов
/ 01 августа 2011

Может случиться так, что когда вы кладете раскладку клавиатуры внизу, она выводится за пределы экрана. Вы можете легко увидеть, так ли это, поместив весь макет в ScrollView.

Я бы также предложил вам указать layout_height клавиатуры, используя значения dp, а не pt. В противном случае он может выглядеть некорректно при различных плотностях экрана.

...