【问题标题】:Big Image Vertical Scroll大图垂直滚动
【发布时间】:2017-06-23 03:03:13
【问题描述】:

我有一个垂直的大图像,我想让它可以滚动。 我是这样弄的,

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/logoimage"
            android:scrollbarAlwaysDrawVerticalTrack="true" >

        <ImageView
            android:contentDescription="Specs"
            android:adjustViewBounds="true"
            android:id="@+id/imageScroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:src="@drawable/rules" >
        </ImageView>
        </ScrollView>

但它不起作用它在布局中显示良好,但是当我启动活动时它不显示

<RelativeLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#172027"
        android:orientation="horizontal"
        android:visibility="visible">
        <ImageView
            android:layout_width="@dimen/logoSize"
            android:layout_height="@dimen/logoSizeH"
            android:src="@drawable/logocopy"
            android:layout_marginTop="25dp"
            android:id="@+id/logoimage"
            android:layout_marginLeft="20dp"/>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/logoimage"
            android:background="@drawable/rules"
            android:scrollbarAlwaysDrawVerticalTrack="true" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <ImageView
                    android:adjustViewBounds="true"
                    android:id="@+id/imageScroll"
                    android:layout_width="match_parent"
                    android:fillViewport="true"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical"
                    android:src="@drawable/rules"
                    android:scaleType="centerCrop" >
                </ImageView>

            </LinearLayout>
        </ScrollView>




        <RelativeLayout
            android:id="@+id/humburger_main1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginTop="30dp"
            android:layout_marginRight="20dp"
            android:layout_alignParentEnd="true"
            android:gravity="center"
            android:orientation="vertical"
            android:layout_alignParentRight="true">

            <LinearLayout
                android:id="@+id/humburger_31"
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:background="#ffff"
                android:orientation="horizontal" />


            <LinearLayout
                android:id="@+id/humburger_11"
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:layout_below="@+id/humburger_31"
                android:layout_marginTop="10dp"
                android:background="#ffff"
                android:orientation="horizontal" />

            <LinearLayout
                android:id="@+id/humburger_21"
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:layout_below="@+id/humburger_31"
                android:layout_marginTop="10dp"
                android:background="#ffff"
                android:orientation="horizontal" />

            <LinearLayout
                android:id="@+id/humburger_41"
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:layout_below="@+id/humburger_21"
                android:layout_marginTop="10dp"
                android:background="#ffff"
                android:orientation="horizontal" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>
        <LinearLayout
            android:id="@+id/RecyclerView1"
            android:layout_width="20dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:background="#00ff0000"
            android:orientation="horizontal"
            android:scrollbars="vertical">

        </LinearLayout>

    </RelativeLayout>

这是活动中的 xml 代码是滚动但图像不显示

【问题讨论】:

  • 你不想水平滚动吗?

标签: android xml android-scroll


【解决方案1】:

试试这个(这段代码来自一个Activity,它在后端有一个xml文件,其中包含一个名为'img'的ImageView)

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"
android:scaleType="center"
android:background="#fff"
android:src="@drawable/picName"
/>

【讨论】:

    【解决方案2】:

    抱歉,您是否在活动中设置了此布局? 使用这个简单的布局而不是你的。

     <ScrollView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
    
     </ScrollView>
    

    【讨论】:

    • ofc 我确实设置为活动
    • 请发送您的活动代码。如果我的回答和其他人没有解决,我认为就我而言,xml 没有任何问题。
    • setContentView(R.layout.activity_savety); omg 我确实设置了内容视图你想要什么?
    【解决方案3】:

    尝试像这样更改ImageView 代码:

    <ImageView
            android:contentDescription="Specs"
            android:adjustViewBounds="true"
            android:id="@+id/imageScroll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="vertical"
            android:src="@drawable/rules"
            android:scaleType="fitXY" >
    

    更新 1

    或者你也可以试试这样的:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/logoimage">
    
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="0dp">
    
            <ImageView
                android:contentDescription="Specs"
                android:adjustViewBounds="true"
                android:id="@+id/imageScroll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/rules"
                android:layout_marginTop="0dp" >
            </ImageView>
        </LinearLayout>
    </ScrollView>
    

    更新 2

    我不确定它是否会起作用,但请尝试将您的布局更改为以下内容:

    <RelativeLayout
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#172027"
        android:visibility="visible">
        <ImageView
            android:layout_width="@dimen/logoSize"
            android:layout_height="@dimen/logoSizeH"
            android:src="@drawable/logocopy"
            android:layout_marginTop="25dp"
            android:layout_alignParentTop="true"
            android:id="@+id/logoimage"
            android:layout_marginLeft="20dp"/>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/scrollview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/logoimage"
            android:background="@drawable/rules"
            android:scrollbarAlwaysDrawVerticalTrack="true" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:adjustViewBounds="true"
                    android:id="@+id/imageScroll"
                    android:layout_width="match_parent"
                    android:fillViewport="true"
                    android:layout_height="wrap_content"
                    android:scrollbars="vertical"
                    android:src="@drawable/rules"
                    android:scaleType="center" >
                </ImageView>
    
            </LinearLayout>
        </ScrollView>
    
        <RelativeLayout
            android:id="@+id/humburger_main1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginTop="30dp"
            android:layout_marginRight="20dp"
            android:layout_alignParentEnd="true"
            android:gravity="center"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@id/scrollview">
    
            <LinearLayout
                android:id="@+id/humburger_31"
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:background="#ffff"
                android:orientation="horizontal" />
    
            <LinearLayout
                android:id="@+id/humburger_11"
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:layout_below="@+id/humburger_31"
                android:layout_marginTop="10dp"
                android:background="#ffff"
                android:orientation="horizontal" />
    
            <LinearLayout
                android:id="@+id/humburger_21"
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:layout_below="@+id/humburger_31"
                android:layout_marginTop="10dp"
                android:background="#ffff"
                android:orientation="horizontal" />
    
            <LinearLayout
                android:id="@+id/humburger_41"
                android:layout_width="30dp"
                android:layout_height="5dp"
                android:layout_below="@+id/humburger_21"
                android:layout_marginTop="10dp"
                android:background="#ffff"
                android:orientation="horizontal" />
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </RelativeLayout>
        <LinearLayout
            android:id="@+id/RecyclerView1"
            android:layout_width="20dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android_layout_toRightOf="@id/scrollview"
            android:background="#00ff0000"
            android:orientation="horizontal"
            android:scrollbars="vertical">
    
        </LinearLayout>
    
    </RelativeLayout>
    

    【讨论】:

    • 如果将ScrollView的android:layout_height属性改为match_parent?
    • 能把整个layout xml文件的代码贴出来吗?
    • 在预览中显示,但在设备上不显示
    • 好的,你能把整个布局xml文件贴出来吗?
    • 我添加了其中的布局
    【解决方案4】:

    这个问题我找了很久,终于找到了这段代码:

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/img"
        android:scaleType="center"
        android:background="#fff"
        android:src="@drawable/picName"/>
    

    还有这个

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    
        setContentView(R.layout.xml_name_layout);
    
        final ImageView switcherView = (ImageView)this.findViewById(R.id.img);
    
        switcherView.setOnTouchListener(new View.OnTouchListener()
        {
    
            public boolean onTouch(View arg0, MotionEvent event)
            {
    
                float curX, curY;
    
                switch (event.getAction())
                {
    
                    case MotionEvent.ACTION_DOWN:
                        mx = event.getX();
                        my = event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        curX = event.getX();
                        curY = event.getY();
                        switcherView.scrollBy((int) (mx - curX), (int) (my - curY));
                        mx = curX;
                        my = curY;
                        break;
                    case MotionEvent.ACTION_UP:
                        curX = event.getX();
                        curY = event.getY();
                        switcherView.scrollBy((int) (mx - curX), (int) (my - curY));
                        break;
                }
    
                return true;
           }
        });
    }
    

    为我完成了完美的工作...包括水平和垂直滚动(启用)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-30
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-29
      • 2011-06-29
      相关资源
      最近更新 更多