【问题标题】:How to make scrollable layouts each one fits screen width?如何使每个可滚动布局都适合屏幕宽度?
【发布时间】:2014-05-28 08:14:19
【问题描述】:

我正在尝试制作具有可滚动 layout 的 android 应用程序,该应用程序可以垂直和水平滚动不同的其他 layouts,我希望这些不同布局的每个布局都适合屏幕宽度并几乎适合高度,看看下图说明了我正在尝试做的事情:

我只是尝试将这些布局的所有 widthheight 设置为 match_parent 并将它们全部放在分配有的 ma​​in_layoutmatch_parent 参数,然后我创建一个 Layout 并将 Layouts(4,1,5) 放入其中并将此 Layout 放入 ScrollView ,然后我将 ma​​in_layout HorizontalScrollView 中的布局,其中包含三个布局(Layout3、Layout(4,1,5)、Layout2),但这给了我对布局的干扰视图。

 <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.9" >

        <!-- just to give it 0.9 of the screen in order to give a space to the top layout (it takes 0.1 and the weight sum is 1) -->

        <LinearLayout
            android:id="@+id/main_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="horizontal" >

            <LinearLayout
                android:id="@+id/Layout3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </LinearLayout>

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="none" >

                <LinearLayout
                    android:id="@+id/Layouts(4,1,5)"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:orientation="vertical" >

                    <LinearLayout
                        android:id="@+id/Layout4"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/Layout1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/Layout5"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >
                    </LinearLayout>
                </LinearLayout>
            </ScrollView>

            <LinearLayout
                android:id="@+id/Layout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </LinearLayout>
        </LinearLayout>
    </HorizontalScrollView>

【问题讨论】:

标签: android android-layout layout


【解决方案1】:

我会尝试:

RelativeLayout 而不是ScrollView

所有LinearLayouts 和match_parent,以编程方式转换:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int sw = size.x; // screen width
int sh = size.y; // screen height

LinearLayout lay2 = (LinearLayout) findViewById(R.id.Layout2);
lay2.setTranslationX = sw;

LinearLayout lay3 = (LinearLayout) findViewById(R.id.Layout3);
lay2.setTranslationX = -sw;

LinearLayout lay4 = (LinearLayout) findViewById(R.id.Layout4);
lay2.setTranslationY = -sh;

LinearLayout lay5 = (LinearLayout) findViewById(R.id.Layout5);
lay2.setTranslationY = sh;

然后触摸RelativeLayout 上的监听器,它可以控制滑动,改变它的位置。

【讨论】:

    【解决方案2】:

    我在link 中找到了我所需要的一切,它提供了一个示例项目源代码,它以一种简单有效的方式(但这种方式只能提供水平导航)通过依赖Android 碎片来实现我需要的方法。 因此,经过搜索,我发现了这个library,它提供了两个方向(垂直和水平)的过程

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2011-05-13
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多