如何固定元素的位置?

How to fix position of an element?

我使用 layout_weight 设置了 ScrollView 的位置,但问题是,我稍后会在其中添加元素,并且 ScrollView 的大小会扩大,所以它会改变位置,我需要它只扩展到底部,但它也扩展到顶部。所以我需要修复它,这样它就不会在顶部展开。我尝试在 dp 中使用直接尺寸而不是重量,但我需要它在不同的分辨率下看起来大致相同。也许还有其他一些方法可以解决问题,而不是使用 layout_height,所以谢谢你的建议。这是 xml 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/chose1"
android:gravity="center_horizontal"
>


<Button
    android:id="@+id/back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=""
    android:layout_marginTop="10dp"
    android:layout_marginLeft="-15dp"
    android:layout_gravity="left"
    android:background="@android:color/transparent"
    android:drawableLeft="@drawable/back"/>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:id="@+id/space"
    >
</LinearLayout>
<Button
    android:id="@+id/button"
    android:layout_width="320dp"
    android:layout_height="2dp"
    android:text=""
    android:layout_marginTop="0dp"/>

<ScrollView
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="0dp"
    android:layout_weight="13"
    android:scrollbars="none">
    <LinearLayout
        android:id="@+id/recipes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        >
    </LinearLayout>
</ScrollView>

作为示例,尝试在滚动视图的线性布局中添加更多按钮并检查这是否是您想要的。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    >
    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_gravity="left"
        android:background="@android:color/transparent"
        />


    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:text=""
        android:background="@android:color/darker_gray"
        android:layout_marginTop="0dp"/>

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

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="match_parent">

            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/darker_gray"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_blue_bright"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_blue_light"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_orange_light"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_green_dark"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/darker_gray"/>
            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_green_light"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/darker_gray"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_blue_bright"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_blue_light"/>

            <Button
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginTop="10dp"
                android:background="@android:color/holo_orange_light"/>


        </LinearLayout>

    </ScrollView>
</LinearLayout>

问题出在滚动视图中的线性布局 wrap_content 并且它会随着您开始在滚动视图中添加内容而变大。

<LinearLayout
    android:id="@+id/recipes"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"/>