如何从屏幕顶部显示相对布局内容

How to display Relative layout content from top of the screen

我在文件 mylayout.xml 中有以下布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/yellow"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="2dp"
    android:id="@+id/Layout5"
    android:layout_alignParentTop="true"
    android:layout_gravity="top"
    android:gravity="top"
    android:orientation="vertical">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/Layout6"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="top">

    <ImageView
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@color/green"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="5dp"
        android:id="@+id/imageView"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_below="@+id/imageView">


        <Button
            android:id="@+id/btn1"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="@string/text_btn_1"
            />

        <Button
            android:id="@+id/btn2"
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:text="@string/text_btn_2"
            />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

当我为具有透明主题的 activity 执行 setContentView(R.layout.mylayout) 时,使用此布局,我的布局显示在屏幕中央。

我想动态更改此布局定位,因此中心(默认)和底部(通过使用以下代码)显示有效。

    LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
    RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
    relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lLayout.setLayoutParams(relativeParas);

但不知何故我无法弄清楚如何将此布局动态显示到屏幕顶部。

如有任何帮助,我们将不胜感激。

尝试删除您之前创建的规则(将其设置为底部对齐),然后添加一个新规则:

//your code.
LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lLayout.setLayoutParams(relativeParas);

//remove rule
relativeParas.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeParas.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lLayout.setLayoutParams(relativeParas);

请注意,从 API 17 起可以使用 removeRule()。

尝试修改以下方式:

layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/Layout5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:background="@color/yellow"
                android:padding="2dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="@color/green"/>

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_below="@+id/imageView"
        android:orientation="horizontal">


        <Button
            android:id="@+id/btn1"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="button1"
            />

        <Button
            android:id="@+id/btn2"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="button2"
            />
    </LinearLayout>
</RelativeLayout>

class:

 RelativeLayout Layout5 = (RelativeLayout)findViewById(R.id.Layout5);
 Layout5.setGravity(Gravity.BOTTOM);

我找到了针对此问题的以下修复方法。 我最后在 Relative layout 下添加了 filler Liner layout。

要居中显示什么都不做。

要在顶部显示,请使用以下代码。只需将填充布局移至底部即可。

    LinearLayout lLayout = (LinearLayout) findViewById(R.id.filler);
    RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
    relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lLayout.setLayoutParams(relativeParas); 

要在底部显示,请使用以下代码。即,将 layout6 移至底部。

    LinearLayout lLayout = (LinearLayout) findViewById(R.id.Layout6);
    RelativeLayout.LayoutParams relativeParas = (RelativeLayout.LayoutParams) lLayout.getLayoutParams();
    relativeParas.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    lLayout.setLayoutParams(relativeParas);