简体   繁体   English

无法在Android的水平scrollview中滚动线性布局

[英]Unable to scroll linear layout inside a horizontal scrollview in android

I am adding few image views inside a horizontal LinearLayout which is again inside a HorizontalScrollView. 我在水平LinearLayout内又在Horizo​​ntalScrollView内添加了一些图像视图。 But the linear layout is not scrolling. 但是线性布局不会滚动。 Below is my code. 下面是我的代码。 Can someone guide me where exactly my xml formation is going wrong? 有人可以指导我xml格式到底出了什么问题吗?

    <?xml version="1.0" encoding="utf-8"?>
    <HorizontalScrollView  xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="15dp"
                    android:layout_marginBottom="2dp"
                    android:fillViewport="true"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:id="@+id/lyt_items"
                        android:layout_width="match_parent"
                        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="@drawable/default_ic" />

                    <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="@drawable/default_ic"  />

                    <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="@drawable/default_ic" />

                    </LinearLayout>
     </HorizontalScrollView>

I would suggest use Constraint Layout As Parent and Inside it Use Nested Scroll View With Linear Layout 我建议使用约束布局作为父级,并在其内部使用带有线性布局的嵌套滚动视图

Here is a sample code, modify according to your need. 这是示例代码,请根据需要进行修改。

<?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"
    tools:context="suraj.iitgandhinagar.Login">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        tools:layout_editor_absoluteX="72dp"
        tools:layout_editor_absoluteY="0dp"
        tools:ignore="MissingConstraints">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="36dp">

            <ImageView
                android:id="@+id/imageView4"
                android:layout_width="272dp"
                android:layout_height="237dp"
                android:layout_gravity="center"
                android:layout_marginTop="40dp"
                android:src="@drawable/unnamed"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="59dp" />


        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

</android.support.constraint.ConstraintLayout>

Take a look at this situation. 看一下这种情况。 if you use the linear layout as match_parent then don't get the layout width. 如果您将线性布局用作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>

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

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