Установите ограничения для HorizontalScrollView - PullRequest
0 голосов
/ 26 июня 2018

У меня есть макет, подобный следующему.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:maxHeight="50dp"
    tools:context=".navigation.NavigationFragment">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/horizontal_scroll_container"
        app:layout_constraintLeft_toLeftOf="parent">

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button1XXX"
                    style="?android:attr/borderlessButtonStyle"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button2"
                    style="?android:attr/borderlessButtonStyle"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button3"
                    style="?android:attr/borderlessButtonStyle"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button4"
                    style="?android:attr/borderlessButtonStyle"/>

                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Button5"
                    style="?android:attr/borderlessButtonStyle"/>

            </LinearLayout>
        </HorizontalScrollView>
    </FrameLayout>

    <FrameLayout
        android:id="@+id/search_layout"
        android:layout_width="58dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/horizontal_scroll_container"/>

</android.support.constraint.ConstraintLayout>

Чего я хочу добиться, так это прокрутить представление прокрутки под розоватым квадратом, чтобы показать последнюю кнопку (в данном случае Button 5).

enter image description here

Можете ли вы помочь мне узнать, что я делаю не так? Я пытался поместить ограничения как в horizontal_scroll_container, так и в HorizontalScrollView, но, похоже, ничего не работает.

P.S. Я не разработчик Android, так что будьте терпеливы со мной;)

1 Ответ

0 голосов
/ 26 июня 2018

Что вы можете сделать, это ограничить ваши FrameLayout справа налево от search_layout, чтобы они не перекрывались. Таким образом, последний Button будет полностью виден.

<FrameLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/horizontal_scroll_container"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@id/search_layout">

Значение width было изменено на 0dp (MATCH_CONSTRAINT), поэтому контейнер ScrollView занимает все доступное горизонтальное пространство.

На самом деле контейнер FrameLayout вообще не нужен. HorizontallScrollView является подклассом FrameLayout, поэтому нет необходимости их вкладывать. Тот же результат может быть достигнут при наличии HorizontalScrollView в качестве прямого потомка ConstraintLayout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:maxHeight="50dp"
    tools:context=".navigation.NavigationFragment">

    <HorizontalScrollView
        android:id="@+id/horizontal_scroll"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@id/search_layout">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button1"
                style="?android:attr/borderlessButtonStyle"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button2"
                style="?android:attr/borderlessButtonStyle"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button3"
                style="?android:attr/borderlessButtonStyle"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button4"
                style="?android:attr/borderlessButtonStyle"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button5"
                style="?android:attr/borderlessButtonStyle"/>

        </LinearLayout>
    </HorizontalScrollView>

    <FrameLayout
        android:id="@+id/search_layout"
        android:layout_width="58dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@id/horizontal_scroll"/>

</android.support.constraint.ConstraintLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...