在Android上两个按钮拉伸到全屏

Chr*_*ian 1 layout android fullscreen grid-layout relativelayout

我觉得这很容易,但找不到我的问题的解决方案:我只想要两个按钮,一个在另一个之上.两者都有屏幕宽度,都有半屏高度,所以它们填满了屏幕.我尝试了不同的RelativeLayout和GridView解决方案,但一切都出错了.

有没有人有我的解决方案?

Sim*_*mon 10

LinearLayout

    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button

        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:text="One" />

    <Button

        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:text="Two" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

关键点是layout_weight和0dp的高度.布局权重将子控件之间的剩余空间分开,因为每个按钮为零像素,所以每个按钮都有50%的可用高度 - 在这种情况下,如果LinearLayout是根,则为全屏.