【问题标题】:make vertical scroll view shrink/expand depending on screen size使垂直滚动视图根据屏幕尺寸缩小/扩大
【发布时间】:2014-09-12 01:06:59
【问题描述】:

我有一个包含 3 个主要元素的布局 - 一个包含一个图像视图、垂直滚动视图和水平滚动视图的布局。 (请参阅下面的 XML:)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".HomeScreen"
    android:weightSum="1">

    <RelativeLayout
        android:id="@+id/RelLayoutTitleImage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">


        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="100dp"
            android:layout_alignParentTop="true"
            android:id="@+id/titleimage"
            android:src="@drawable/saferroadsshellharbourtitle"
            android:layout_weight="0.08" />
    </RelativeLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="272dp"
        android:id="@+id/scrollView"
        android:layout_gravity="center"
        >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="272dp"
            android:orientation="vertical">

            <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:id="@+id/btn_Old_logo"
                android:clickable="true"
                android:src="@drawable/oldlogomenu"/>

            <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:id="@+id/btn_report_a_hazard"
                android:layout_below="@+id/btn_Old_logo"
                android:clickable="true"
                android:src="@drawable/reportahazardmenu"/>

            <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_below="@+id/btn_report_a_hazard"
                android:id="@+id/btn_Council_Website"
                android:clickable="true"
                android:src="@drawable/councilwebsitemenu"/>

            <ImageButton
                android:layout_width="fill_parent"
                android:layout_height="100dp"
                android:layout_below="@+id/btn_Council_Website"
                android:id="@+id/btn_dob_in_a_hoon"
                android:clickable="true"
                android:src="@drawable/dobinahoonmenu"/>

        </RelativeLayout>
    </ScrollView>


    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/horizontalScrollView2"
        android:layout_gravity="bottom">

        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="120dp"
            android:layout_height="90dp"
            android:gravity="bottom"
            >


            <ImageButton
                android:layout_width="120dp"
                android:layout_height="90dp"
                 android:id="@+id/facebook"
                android:layout_gravity="center_horizontal"
                android:clickable="true"
                android:src="@drawable/facebookbutton"
                android:scaleType="fitCenter"/>

            <ImageButton
                android:layout_width="120dp"
                android:layout_height="90dp"
                android:layout_toRightOf="@+id/facebook"
                android:id="@+id/twitter"
                android:layout_gravity="center_horizontal"
                android:clickable="true"
                android:src="@drawable/scclogoold"
                android:scaleType="fitCenter"/>

            <ImageButton
                android:layout_width="120dp"
                android:layout_height="90dp"
                android:layout_toRightOf="@+id/twitter"
                android:id="@+id/contact"
                android:layout_gravity="center_horizontal"
                android:clickable="true"
                android:src="@drawable/contactbutton"
                android:scaleType="fitCenter"/>

        </RelativeLayout>


    </HorizontalScrollView>


</LinearLayout>

我想做的是将垂直滚动视图的上边缘“固定”到图像视图的下边缘,将垂直滚动视图的下边缘“固定”到水平滚动视图的上边缘。所以垂直滚动视图会根据设备的屏幕大小而扩大和缩小。

有人可以解释一下这是如何实现的吗?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    这是通过在布局中使用 layout_weight 和 WeightSum 来实现的。它将响应例如我有一个带有 2 个按钮的水平线性布局我将水平布局的 weightSum 设置为 2 并将按钮 layout_weight 设置为 1 这将导致按钮共享 50% 50% 的布局它会响应设备的尺寸。

    【讨论】:

    • 还请注意,一旦添加了 layout_weight 以响应 weightSum,您应该将 layout_width 设为 0。
    • 非常感谢!在一个单独但相关的问题上——你能对视图的宽度做同样的事情吗?
    • 当然,只要 layout_weight 可用于小部件。使用布局作为 weightSum。甚至布局也可以有 layout_weight。
    猜你喜欢
    • 1970-01-01
    • 2021-05-01
    • 2012-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多