【问题标题】:How to set the linear layout width in a HorizontalScrollView?如何在 Horizo​​ntalScrollView 中设置线性布局宽度?
【发布时间】:2013-12-31 14:46:00
【问题描述】:

我有一个LinearLayout(我们称之为A),它的宽度设置为fill_parent,这个布局包含一堆另一个LinearLayouts(我们称它们为B),我希望屏幕只显示 四个 B,所以我将 Aweight_sum 分配为 4 和将每个 B 分配为 1,现在我想添加一个 HorizontalScrollView,这样如果我有 6 个 B 布局,则只会显示四个其他两个将滚动。我构建了这个 HorizontalScrollView 以包含 A 布局(因为 HorizontalScrollView 应该只有一个直接子级),现在我添加到布局 A 的 fill_parent 宽度是毁了,因为它现在服从 HorizontalScrollView 所以 B 布局宽度也毁了,看看下面的图:

-- 黄色:全屏。

-- green : A 布局。

-- red : B 布局。

我得到的结果:

我想得到的结果:

我尝试将 HorizontalScrollView 和布局 A 的宽度设置为 fill_parent 和/或 wrap_content,但对我没有任何帮助。

我的 XML 代码:

      <HorizontalScrollView 
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content" >
         <LinearLayout 
                       android:id="@+id/A"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content"
                       android:orientation="horizontal"
                       android:weightSum="4" >

            <LinearLayout
                    android:id="@+id/B1"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <LinearLayout
                        android:id="@+id/icon1"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.5"
                        android:background="@drawable/ac_overlay"
                        android:orientation="horizontal"
                        android:tag="normal" >
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/icon2"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.5"
                        android:background="@drawable/ac_overlay"
                        android:orientation="horizontal"
                        android:tag="normal" >
                    </LinearLayout>
                </LinearLayout>
             <LinearLayout
                    android:id="@+id/B2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:orientation="vertical"
                    android:weightSum="1" >

                    <LinearLayout
                        android:id="@+id/icon3"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.5"
                        android:background="@drawable/ac_overlay"
                        android:orientation="horizontal"
                        android:tag="normal" >
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/icon4"
                        android:layout_width="wrap_content"
                        android:layout_height="0dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:layout_marginTop="15dp"
                        android:layout_weight="0.5"
                        android:background="@drawable/ac_overlay"
                        android:orientation="horizontal"
                        android:tag="normal" >
                    </LinearLayout>
                  </LinearLayout>
                 </LinearLayout>
    </HorizontalScrollView>

【问题讨论】:

  • 不是你的问题的真正答案,可能不会解决你的问题,但我不能停下来问这个,你确定你不能避免在其他布局中使用这么多布局吗?好像很多透支
  • @MariusBudin 是的,看起来正在使用 LinearLayouts 而不是 ImageViews 或 ImageButtons。
  • @MariusBudin 这些图标布局不仅仅是带有 bg 的布局,我只是不想用不必要的代码填充问题,每个布局包含三个 ImageButtons 和一个 TextView。

标签: android android-layout android-linearlayout horizontalscrollview


【解决方案1】:

您无法仅从 xml 中完成此操作,您需要一些动态代码来测量屏幕的宽度,然后以编程方式将每个线性布局(icon1、icon2 等)的宽度设置为 1/这个宽度的 4 个。

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(metrics.widthPixels/4, LayoutParams.MATCH_PARENT);

LinearLayout icon1 = (LinearLayout) findViewById(R.id.icon1);
icon1.setLayoutParams(params);

//etc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-31
    • 2014-02-13
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-04
    • 2021-12-27
    相关资源
    最近更新 更多