希望按钮在 android 的滚动视图中使用全屏

Want Buttons to use the full screen in scroll view in android

我想在 activity 中放置 9 个按钮,有两个条件:

1.If 屏幕很小,按钮滚动。

2.If 屏幕足够大,可以一次容纳所有这些,然后它们会被拉伸以填满屏幕。

我试过了,但没用。谢谢。

screenshot of layout

<include
    android:id="@+id/toolbar"
    layout="@layout/appbar"></include>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/toolbar">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fff0fd"
            android:orientation="vertical"
            android:layout_margin="8dp">



            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/First_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Second_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:onClick="onButtonClick"
                android:text="@string/Third_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Forth_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Fifth_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Sixth_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button7"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Seventh_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Eighth_Semester"
                android:textStyle="bold" />

            <Button
                android:id="@+id/button9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:text="@string/Gate"
                android:textStyle="bold" />

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

您必须为它们制作不同的布局,方法是在您的 res 文件夹中创建名为 layout-small 和 layout-large 的文件夹,如下所述:Supporting Multiple Screens

当然会使用名为 'layout' 的文件夹。但是,当您创建一个名为 layout-small 的文件夹时,如果屏幕尺寸小于 470dp x 320dp(这是 layout-normal 的最小分辨率),将使用该文件夹。

关于拉伸,使用线性布局中的布局权重和权重总和,以获得看起来适合预览的布局。

去掉权重参数,将layout_height设置为wrap_content,像这样:

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff0fd"
            android:orientation="vertical"
            android:layout_margin="8dp">

        <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/First_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Second_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="onButtonClick"
                android:singleLine="true"
                android:text="@string/Third_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Forth_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Fifth_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Sixth_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button7"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Seventh_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button8"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Eighth_Semester"
                android:textStyle="bold"/>

        <Button
                android:id="@+id/button9"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true"
                android:text="@string/Gate"
                android:textStyle="bold"/>

    </LinearLayout>
</ScrollView>

您可以在编辑器中预览不同的屏幕分辨率,以确定使用或不使用滚动视图的情况: