我在文件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>
通過這一佈局,當我在做的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);
但不知何故,我無法弄清楚如何動態顯示此佈局到屏幕的頂部。
任何幫助將不勝感激。
使Android:layout_width和Android:layout_height等於match_parent –
嘗試 「relativeParas.addRule(RelativeLayout.ALIGN_PARENT_TOP);」 – Nilabja
只需定義清單中的主題,以便從哪個活動中顯示來自Top的內容。它刪除標題欄並覆蓋整個屏幕。 android:theme =「@ android:style/Theme.Black.NoTitleBar」,並將layout_width和Height屬性設置爲xml文件中的match_parent或fill_parent。 – Tara