Layout changes a lot when orientation changes

I created a layout for the action. The layout contains some image buttons and textual representations. The problem is that when I adjust the orientation of my device to a portrait, its work is great, but when I change the orientation to landscape, the layout gives unexpected results.

Here is my layout file:

<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> 

Here is the result of portrait mode on the device , and this is the result that is expected in both portrait and landscape modes:

Portrait mode

Here is the result for landscape mode on the device , it is unexpected:

Landscape mode

I want the layout to look the same in portrait and landscape modes.

+1
source share
4 answers

- It seems you screen size creating a problem here.

- You will need to create another folder in the res directory as layout-land , and in it create a .xml file just like in layout and make it look the way you want in landscape mode .

- When your application goes into landscape mode, automatically , this .xml file will be selected (i.e. from the layout folder).

+1
source

Due to the lower height in landscape mode, the layout will be displayed as follows.

In my opinion, a good way is to save the layout in the res folder and create a layout for landscape mode. so it will be a beautiful appearance

+1
source
  • create a folder / res / layout -land (here you save your landscape adjusted layouts).
  • copy selectActivity.xml there
  • make the necessary changes to it
+1
source

first you need to create the following folder for different layout .....

 Folder Name layout-ldpi layout-land-ldpi layout-mdpi layout-land-mdpi layout-hdpi layout-land-hdpi layout-xlarge layout-xlarge-land 

1 ... Change in AndroidManifest.xml

 android:configChanges="orientation|keyboardHidden" 

2 .... Create layout_land.xml in the layout folder.

3 .... Create layout_port.xml in the layout folder.

4 ... Create the next method

 @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 .... Change the OnCreate method (this content only)

 @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 ... Create a set_all_id () method to initialize objects of the form

 public void set_all_id() { //define id for all view objects.. //textview = (textview)findViewById(R.id.textview); } 
+1
source

Source: https://habr.com/ru/post/1447707/


All Articles