Android: Positioning buttons when turned from portrait to landscape

瘦欲@ 提交于 2020-01-13 22:39:42

问题


I have a menu-activity, in which I have several positioned buttons. It fits my phone screen perfectly when in portrait mode, but in landscape mode it's destroyed. I know that when I have fixed positioning this will be the result. Can anyone tell me how to position my buttons so my activity keeps its form also in landscape mode? Thank you.

Here is my xml file:

  <?xml version="1.0" encoding="utf-8"?>
 <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="match_parent"
  android:orientation="vertical" >


<Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/bezpecnost_over"
     android:layout_marginLeft="30dp"
     android:layout_marginTop="50dp"


      />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/kurenie_over"
    android:layout_marginLeft="200dp"
    android:layout_marginTop="50dp"

     />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/osvetlenie_over"
    android:layout_marginLeft="200dp"
    android:layout_marginTop="200dp" 
    android:onClick="obrOsv"    />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:background="@drawable/pohodlie_over"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="200dp"
    />

 </RelativeLayout>

回答1:


It fits my phone screen perfectly when in portrait mode, but in landscape mode it's destroyed.

This is expected for relative-based layouts. What developers usually do in this scenario:

  • Make a folder under res called layout-land
  • Make an xml with the same name as the layout in portrait mode (contained in res/layout)
  • Position for landscape mode.

Since the xml file name is the same, when it is time for the activity to set the content view

setContentView (R.layout.my_layout);

The my_layout.xml file from the layout-land folder will be used instead.

Also read Layouts & Supporting Multiple Screens from the Android docs, they both have useful information with the latter talking about the different qualifiers (such as layout-land).




回答2:


So you're using a relative layout to space out buttons horizontally by using increasing amounts of margin? That's totally not understanding how layouts work. Start by replacing the RelativeLayout with a LinearLayout with orientation set to horizontal, and get rid of all the margins- margins should just be a few pixels between elements. I'd suggest you read the Android tutorials on how layouts work.



来源:https://stackoverflow.com/questions/14842847/android-positioning-buttons-when-turned-from-portrait-to-landscape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!