[英]Layout change drastically on orientation change
我已經為活動創建了布局。 該布局包含一些圖像按鈕和文本視圖。 問題是,當我將設備的方向設置為縱向時,它可以正常工作,但是當我將方向更改為橫向時,布局給出了意外的結果。
這是我的布局文件 :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:padding="50dp" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/settings_imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@android:drawable/bottom_bar" />
<TextView
android:id="@+id/settings_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="@string/my_settings"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/myProgramming_imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/settings_imageView"
android:layout_alignParentLeft="true"
android:src="@android:drawable/bottom_bar" />
<TextView
android:id="@+id/myProgramming_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/myProgramming_imageView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="@string/my_programming"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/library_imageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/myProgramming_imageView"
android:layout_alignParentLeft="true"
android:src="@android:drawable/bottom_bar" />
<TextView
android:id="@+id/library_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/library_imageView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="16dp"
android:text="@string/library"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</ScrollView>
這是設備上的縱向模式的輸出,這是縱向和橫向模式下的預期輸出:
這是設備上 橫向 模式的輸出,這是意外的:
我希望布局在縱向和橫向模式下看起來相似。
首先,您必須為不同的布局創建以下文件夾.....
Folder Name
layout-ldpi
layout-land-ldpi
layout-mdpi
layout-land-mdpi
layout-hdpi
layout-land-hdpi
layout-xlarge
layout-xlarge-land
1 ...更改AndroidManifest.xml
android:configChanges="orientation|keyboardHidden"
2 ....在布局文件夾中創建layout_land.xml。
3 ....在layout-land文件夾中創建layout_port.xml。
4 ...創建后續方法
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.layout_land);
set_all_id();
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.layout_port);
set_all_id();
}
}
5 ....更改OnCreate方法(僅此內容)
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation == 1)
{
setContentView(R.layout.layout_port);
}
if (getResources().getConfiguration().orientation == 2)
{
setContentView(R.layout.layout_land);
}
set_all_id();
}
6 ...創建set_all_id()方法以初始化視圖對象
public void set_all_id()
{
//define id for all view objects..
//textview = (textview)findViewById(R.id.textview);
}
由於landscape
模式下的高度較小,因此布局將像這樣顯示。
據我說,好的方法是在res文件夾中維護layout-land,並為landscape模式設計布局。 這樣看起來會很漂亮
-似乎您的screen size
在這里造成了問題。
-您將必須在 res
目錄下創建另一個文件夾 layout-land
,並在其中創建一個.xml
文件,就像在layout
文件夾中一樣,並使其在landscape mode
看起來像您想要的樣子。
-當您的應用進入landscape
模式時,該.xml
文件將自動被選中(即從layout-land)文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.