如何在 Android 中使用滚动视图 [英] How to use Scroll View In Android

查看:34
本文介绍了如何在 Android 中使用滚动视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为两种不同的布局使用了两个滚动视图.这两个布局在一个线性布局中.

I have use two scroll views for two different layout. And those two layouts are inside a Linear Layout.

这是我的 XML 文件.我不明白为什么 ScrollView 对我不起作用

Here is my XML file. I don't why ScrollView is not working for me

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal" >

    <LinearLayout
            android:id="@+id/flightResultData"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:orientation="vertical" >

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

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="horizontal" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >

                        <ImageView
                            android:id="@+id/onewayflightLogo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:src="@drawable/spicejet" />

                        <TextView
                            android:id="@+id/onewayflightName"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="SpiceJet" />

                        <TextView
                            android:id="@+id/onewayflightNumber"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="9W - 496" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_marginTop="8dp"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/onewayflightTime"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="06:00 - 7:05"
                            android:textSize="12dp" />

                        <TextView
                            android:id="@+id/onewayflightDuration"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="1h 35m | Non Stop"
                            android:textSize="10dp" />
                        <TextView
                            android:id="@+id/onewayflightAmount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="Rs 200000"
                            android:textSize="12dp" />
                    </LinearLayout>
                </LinearLayout>
                <View
                    android:layout_width="1dp"
                    android:layout_height="60dp"
                    android:background="@android:color/darker_gray" />
               <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="horizontal" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >

                        <ImageView
                            android:id="@+id/retrunflightLogo"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="3dp"
                            android:src="@drawable/spicejet" />

                        <TextView
                            android:id="@+id/retrunflightName"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="SpiceJet" />

                        <TextView
                            android:id="@+id/retrunflightNumber"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="9W - 496" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_marginTop="8dp"
                        android:layout_width="fill_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/retrunflightTime"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="06:00 - 7:05"
                            android:textSize="12dp" />

                        <TextView
                            android:id="@+id/retrunflightDuration"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="1h 35m |Non Stop"
                            android:textSize="10dp" />

                        <TextView
                            android:id="@+id/retrunflightAmount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:minLines="1"
                            android:text="Rs 200000"
                            android:textSize="12dp" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
</LinearLayout>

这是我的主要布局,其中我通过循环动态地膨胀我上面的 xml

This is my main layout in which I am inflating my above xml dynamically through a loop

<LinearLayout
        android:id="@+id/flightResultData"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sortFlightLayouts"
        android:layout_marginLeft="8dp"
        android:orientation="vertical" >
</LinearLayout>

在我的活动代码中,我正在这样做:

And in my activity code I am doing this:

void setTestResultData(){

    flightResult=(LinearLayout)findViewById(R.id.flightResultData);
    LinearLayout.LayoutParams flightDetailsLayout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout.LayoutParams forUnderLine = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    forUnderLine.setMargins(0,0, 0, 0);
    for(int i=0;i < 13;i++){
        LinearLayout flightInformations=(LinearLayout)inflater.inflate(R.layout.flight_details_layout, null);
        flightResult.addView(flightInformations);
    }
}

推荐答案

我认为您需要一个简单的两栏式组织:

I think you need a simple, two-column organization:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <!-- stuff goes here that should appear above the scrolling areas -->

    <ScrollView
        android:id="@+id/left_side_scroller"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- contents of left side go here -->
    </ScrollView>

    <ScrollView
        android:id="@+id/right_side_scroller"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <!-- contents of right side go here -->
    </ScrollView>

    <!-- stuff goes here that should appear below  the scrolling areas -->
</LinearLayout>

或者(也许更好)看起来您应该使用两个 ListView 元素而不是两个 ScrollView 元素.每个 ListView 将具有与上述 ScrollView 相同的布局参数.由于 ListView 在内部管理滚动,因此您根本不需要 ScrollView.

Alternatively (and perhaps better) it looks like you should be using two ListView elements instead of two ScrollView elements. Each ListView would have the same layout parameters as shown above for the ScrollView. Since a ListView manages scrolling internally, you then don't need ScrollView at all.

此外,您可能希望整个布局填满屏幕,过滤器"和排序"元素始终位于底部.为了达到这个效果,顶层布局应该有 android:layout_height="fill_parent" 而不是 "wrap_content".此外,可滚动区域应该具有 android:layout_height="0dp" 和非零权重(他们已经这样做了).

Also, you probably want the entire layout to fill the screen, with the "filter" and "sort" elements always at the bottom. To achieve this effect,the top-level layout should have android:layout_height="fill_parent" instead of "wrap_content". Also, the scrollable areas should have android:layout_height="0dp" and a non-zero weight (which they already do).

这篇关于如何在 Android 中使用滚动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆