【问题标题】:How to use Scroll View In Android如何在 Android 中使用滚动视图
【发布时间】:2013-08-05 09:26:16
【问题描述】:

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

这是我的 XML 文件。我不明白为什么ScrollView 不适合我

<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

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

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

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);
    }
}

【问题讨论】:

  • 不能用列表视图代替吗?
  • @AndroSelva 不,我必须使用线性布局查看我发布的图片链接,你会很容易理解我必须做什么
  • @Gaurav 使用列表视图将线性布局添加为页眉和页脚。我之前也提出过同样的建议,现在我也提出了同样的建议。实际上使用gridview。将页眉和页脚添加到gridview。推荐使用gridview。
  • @Raghunandan 请查看我添加的图像链接。我如何使用 listview 执行此操作。航班详细信息在不同的布局中,因为您看到我在运行时对其进行充气的代码跨度>
  • 我只想在两边都有一个滚动视图,每个布局独立滚动

标签: android android-layout android-scrollview android-scroll


【解决方案1】:

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

<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

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

【讨论】:

  • 我是 android 新手,您能帮我提供一些示例代码吗?我如何将 listview 用于我的设计,左右两侧有两种布局
  • @Gaurav - 查看guide topic on List View 以了解ListView 的工作原理。它将教您如何使用单个ListView 进行布局。 (但是,您不需要ListActivity,因为它只处理单个ListView。所以不要遵循指南的那部分内容。)然后只需用两个ListViews 代替@987654338 @s 我的回答中有。您最终将得到一个布局,该布局具有两个独立滚动的行列表,如您所愿。这有点复杂,但您的要求也是如此。
【解决方案2】:

如果您在水平线性布局中显示滚动视图并为滚动视图赋予权重,则将滚动视图的宽度设置为“0dp”

android:layout_width="0dp"

如果是垂直的,那么

android:layout_height="0dp"

还设置您的主要 LinearLayout 高度以填充父级

【讨论】:

  • 这么简单的横向滚动视图
  • 我需要两个布局的垂直滚动视图
  • 只需粘贴你的屏幕或从头开始......这样我也更容易理解和流畅
  • 更新了我的问题,请查看代码,我希望它们进入滚动视图的两个细节之间有一个边框
猜你喜欢
  • 1970-01-01
  • 2010-12-20
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多