简体   繁体   English

如何设计水平滚动的自定义视图

[英]How to design custom view which is horizontally scrollable

在此处输入图片说明

How to design this kind of view, it should be horizontally scrollable and fall in the range of that week bar also 如何设计这种视图,它应该可以水平滚动并落在那个星期的范围之内

If you are using the LinearLayout as match_parent then don't get the layout width. 如果您将LinearLayout用作match_parent则不会获得布局宽度。 Use wrap_content . 使用wrap_content If its don't work then set the fixed size for your image. 如果它不起作用,则为图像设置固定尺寸。

<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>

Update: if you want to generate this view dynamically then follow these steps 更新:如果要动态生成此视图,请按照以下步骤操作

1) put this layout where you want to view this scroll able functionality 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) make custom layout which want to show in Scrollbar 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) Use this java code to generate views at runtime 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);

        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM