Как создать собственный вид с горизонтальной прокруткой - PullRequest
0 голосов
/ 26 октября 2018

enter image description here

Как спроектировать вид такого вида, он должен быть с горизонтальной прокруткой и также попадать в диапазон этой недели

1 Ответ

0 голосов
/ 26 октября 2018

Если вы используете LinearLayout в качестве match_parent, тогда не получите ширину макета.Используйте wrap_content.Если это не работает, тогда установите фиксированный размер для вашего изображения.

<HorizontalScrollView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="15dp"
    android:layout_marginBottom="2dp"
    android:fillViewport="true"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/lyt_items"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/iv_icon1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:contentDescription="@string/app_name"
            android:layout_marginRight="1dp"
            android:src="@mipmap/ic_launcher" />
        <ImageView
            android:id="@+id/iv_icon2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:contentDescription="@string/app_name"
            android:layout_marginRight="1dp"
            android:src="@mipmap/ic_launcher" />
        <ImageView
            android:id="@+id/iv_icon3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:contentDescription="@string/app_name"
            android:layout_marginRight="1dp"
            android:src="@mipmap/ic_launcher" />
    </LinearLayout>
</HorizontalScrollView>

Обновление: , если вы хотите сгенерировать это представление динамически, выполните следующие шаги

1) поместите этот макет туда, где вы хотите просмотреть эту функциональность с возможностью прокрутки

<LinearLayout
                android:layout_width="0dp"
                android:layout_height="70dp"
                android:layout_weight="1">

                <HorizontalScrollView
                    android:id="@+id/horizontal_scrollview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="2dp"
                    android:layout_marginTop="2dp"
                    android:scrollbars="none">

                    <LinearLayout
                        android:id="@+id/image_layout"
                        android:layout_width="72dp"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:orientation="horizontal" />
                </HorizontalScrollView>

            </LinearLayout>

2) создайте собственный макет, который хотите показать на полосе прокрутки

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="72dp"
    android:layout_height="70dp"
    android:background="@drawable/circle_selector"
    android:gravity="center"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/image_alert_icon_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:background="@drawable/circle_selector">

        <ImageView
            android:id="@+id/imageView_device"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_fences" />

    </RelativeLayout>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxLength="10"
        android:text="name"
        android:textColor="@color/white"
        android:textSize="12sp" />
</LinearLayout>

3) Используйте этот Java-код для генерации представлений во время выполнения

private void addImagetoLayout( {
        imageLayout.removeAllViews();

        for (int i = 0; i < list.size(); i++) {
            View view = getLayoutInflater().inflate(R.layout.image_layout, null);
            RelativeLayout imageAlertLayout = (RelativeLayout) view.findViewById(R.id.image_alert_icon_layout);
            ImageView imageView = (ImageView) imageAlertLayout.findViewById(R.id.imageView_device);
            TextView name = (TextView) view.findViewById(R.id.name);
            // set image in imageView
       // set text in text View
            imageLayout.addView(view);

        }

    }
...