在Android中具有大于屏幕权重的可滚动LinearLayout

11

我正在尝试创建一个垂直的线性布局,其权重比屏幕大两倍。为了让这个布局正常工作,我需要能够滚动它。不幸的是,我无法找到一种方法来实现这一点。我尝试使用布局权重,并将权重总和设置为所有组件权重的实际总和的一半(因此,如果所有组件权重总和为20,则将权重总和设置为10),并设法使其工作,但不幸的是滚动不再起作用。

我是否漏掉了什么?

以下是使线性布局大小是屏幕大小两倍的代码,但滚动不起作用:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="2">

        <EditText android:id="@+id/id1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_weight="2"/>

        <EditText android:id="@+id/id2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:textSize="25dp"
            android:gravity="center"
            android:layout_weight="2"/>

    </LinearLayout>
</ScrollView>
3个回答

8
根据您的评论,以下是实现您所需的编程方式。我不认为在纯布局XML中能够完成这个任务。
布局:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="#000000"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/id1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#DCDCDC"
            android:gravity="center"
            android:text="ONE ONE ONE"
            android:textIsSelectable="false"
            android:textSize="45sp" />

        <EditText
            android:id="@+id/id2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#AAAAAA"
            android:gravity="center"
            android:text="TWO TWO TWO"
            android:textIsSelectable="false"
            android:textSize="45sp" />

        <EditText
            android:id="@+id/id3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#777777"
            android:gravity="center"
            android:text="THREE THREE THREE"
            android:textIsSelectable="false"
            android:textSize="45sp" />
    </LinearLayout>

</ScrollView>

活动:

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      // Get display size -- API L13 and up. Otherwise use getResources().getDisplayMetrics();
      Display display = getWindowManager().getDefaultDisplay();
      Point size = new Point();
      display.getSize(size);


      // You want size to be 50% per EditText, so divide available height by 2.
      // Note: this is absolute height, does not take into consideration window decoration!
      int editTextHeight = size.y / 2;

      // Get a handle to your EditTexts
      EditText t1 = (EditText) findViewById(R.id.id1);
      EditText t2 = (EditText) findViewById(R.id.id2);
      EditText t3 = (EditText) findViewById(R.id.id3);

      // Set height to 50% of screen size each
      t1.setHeight(editTextHeight);
      t2.setHeight(editTextHeight);
      t3.setHeight(editTextHeight);
}

那就这样吧。最终结果为:

EditText高度为屏幕高度的50% EditText高度为屏幕高度的50% EditText高度为屏幕高度的50%


2
谢谢回复。我知道如何通过编程实现,但即使仅仅确认无法在纯XML中完成,这个赏金也绝对值得。 - sakis kaliakoudas

3
你声明了你的 LinearLayout 的高度为 "match_parent",这意味着它的高度与其父视图相同。只要内容大于 ScrollView,它就永远不会滚动。首先,你需要给它一个固定的高度(如 50dp)或者使用 wrap_content,或者通过编程设置其高度(比如 2 倍屏幕高度)。weightSumweight 将始终强制你的项目适合当前 LinearLayouts 的大小,因此尽量不要使用它们。希望这可以帮到你。

1
问题在于你使用的 layout_weightweightSum 是无效的。重要的是要记住,android:layout_weight 只能使用视图中剩余可用空间;超出该边界的任何内容都会自动裁剪。
因此,在你的示例中,第一个 EditText 占据了整个屏幕,第二个则完全排除在视图之外。由于第二个 EditText 被裁剪,LinearLayout 已经占据了整个屏幕,而 ScrollView 没有做任何事情。
我不确定你的最终目标是什么;你是否想要随着用户输入而增长的文本输入,并且 ScrollView 处理溢出?
如果是这样,这将起作用:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal"
    android:background="#000000"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:orientation="vertical" >

        <EditText
        android:id="@+id/id1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#DCDCDC"
        android:gravity="center"
        android:text="ONE ONE ONE"
        android:textIsSelectable="false"
        android:textSize="45sp" />

    <EditText
        android:id="@+id/id2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#AAAAAA"
        android:gravity="center"
        android:text="TWO TWO TWO"
        android:textIsSelectable="false"
        android:textSize="45sp" />
    </LinearLayout>

</ScrollView>

我已经包含了一些基本的背景颜色,以便您可以在布局预览中看到每个项目的开始和结束位置。在第一个EditText中输入将正确地将第二个推下来,从而产生滚动条。
还请注意,在textSize值方面应使用sp而不是dp
编辑:我还应该说明一点,即weightSum在必要时也会减少空间。为了测试这一点,将您的LinearLayoutweightSum设置为2,然后向每个EditText控件添加android:layout_weight="1"。最终结果将是视图加载时的50/50分割,然后当您开始在第一个控件中键入时,第二个空间将相应缩小。向第二个控件添加文本将导致滚动条出现。

你好。感谢回复。基本上,我想要实现的一个很好的例子就是有3个文本视图,每个视图大小为屏幕大小的1/2。这意味着前两个会出现在屏幕上,第三个只有在向下滚动时才会出现。 - sakis kaliakoudas
1
在纯XML中无法实现这一点;您需要测量屏幕并在代码中设置视图高度。我可以为您提供一个示例。 - Mike P.
1
@shijuB,那个方法不行,请看我上面的回答;按照你说的做会导致布局填满视口而无法滚动。再加一个第三个“EditText”,它将无法在屏幕外访问。 - Mike P.
1
@shijuB 再次强调,那样做不行。那只会将每个EditText的视口填充到50%,并且不允许滚动。 - Mike P.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接