0

Проблема с кнопками которые в портретном режиме выглядят нормально

А в Лэндскейпе уходят в сторону

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#242a30"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">


        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true">

            <Button
                android:id="@+id/btn1"
                android:layout_width="125dp"
                android:layout_height="125dp"
                android:layout_column="0"
                android:layout_marginLeft="47dp"
                android:layout_marginTop="50dp"
                android:layout_row="0"
                android:background="#fb6d59"
                android:text="Button"
                android:textColor="@android:color/background_light" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="125dp"
                android:layout_height="125dp"
                android:layout_column="0"
                android:layout_marginLeft="47dp"
                android:layout_marginTop="15dp"
                android:layout_row="1"
                android:background="#3cc1a4"
                android:text="Button"
                android:textColor="@android:color/background_light" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="125dp"
                android:layout_height="125dp"
                android:layout_column="1"
                android:layout_marginLeft="17dp"
                android:layout_marginRight="47dp"
                android:layout_marginTop="50dp"
                android:layout_row="0"
                android:background="#79aa37"
                android:text="Button"
                android:textColor="@android:color/background_light" />

            <Button
                android:id="@+id/btn4"
                android:layout_width="125dp"
                android:layout_height="125dp"
                android:layout_column="1"
                android:layout_marginLeft="17dp"
                android:layout_marginRight="47dp"
                android:layout_marginTop="15dp"
                android:layout_row="1"
                android:background="#1893fd"
                android:text="Button"
                android:textColor="@android:color/background_light" />

        </GridLayout>

    </LinearLayout>

</RelativeLayout>

введите сюда описание изображения введите сюда описание изображения

2 ответа 2

1

Вы им задаете конкретный размер. Вот они и не растягиваются на все доступное пространство. Для того, чтобы они использовали все доступное пространство - используйте атрибут layout_weight.

2
  • weight не помогает
    – Anton
    16 апр 2017 в 20:46
  • @Anton, дык выложите код с weight. 16 апр 2017 в 20:48
1

При желании вы можете создать отдельный layout файл с тем же именем, но в папке layout-land (правый клик по ресурсам -> new -> Android recource directory -> Recource type выбрать layout, в столбце Available qualifiers - orientetion нажать кнопку со стрелкой, в выпадающем списке выбрать landscape) и написать в этом файле отдельное описание для landscape ориентации. При повороте устройства - будет выбираться нужный layout автоматически. Но на самом деле даже в портретной ориентации кнопки расположены не по центру. (попробуйте на разных устройствах - это сильно заметно)

А вообще посоветовал бы вам пользоваться по началу ConstraintLayout: его можно рисовать, а не прописывать руками. Вот приблизительный код, который во всех ориентациях будет нормально смотреться:

<android.support.constraint.ConstraintLayout
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:orientation="vertical"
   android:layout_alignParentTop="true"
   android:layout_alignParentLeft="true"
   android:layout_alignParentStart="true"
   xmlns:android="http://schemas.android.com/apk/res/android">


   <Button
       android:id="@+id/button"
       android:layout_width="111dp"
       android:layout_height="111dp"
       android:text="Button"
       android:layout_marginBottom="8dp"
       app:layout_constraintBottom_toTopOf="@+id/guideline2"
       app:layout_constraintRight_toLeftOf="@+id/guideline"
       android:layout_marginRight="8dp"/>

   <Button
       android:id="@+id/button2"
       android:layout_width="111dp"
       android:layout_height="111dp"
       android:text="Button"
       app:layout_constraintBaseline_toBaselineOf="@+id/button"
       android:layout_marginLeft="8dp"
       app:layout_constraintLeft_toLeftOf="@+id/guideline"/>

   <Button
       android:id="@+id/button3"
       android:layout_width="111dp"
       android:layout_height="111dp"
       android:text="Button"
       app:layout_constraintTop_toTopOf="@+id/guideline2"
       android:layout_marginTop="8dp"
       app:layout_constraintRight_toLeftOf="@+id/guideline"
       android:layout_marginRight="8dp"/>

   <Button
       android:id="@+id/button4"
       android:layout_width="111dp"
       android:layout_height="111dp"
       android:text="Button"
       app:layout_constraintBaseline_toBaselineOf="@+id/button3"
       android:layout_marginLeft="8dp"
       app:layout_constraintLeft_toLeftOf="@+id/guideline"/>

   <android.support.constraint.Guideline
       android:layout_width="1dp"
       android:layout_height="1dp"
       android:id="@+id/guideline"
       android:orientation="vertical"
       app:layout_constraintGuide_percent="0.5"/>

   <android.support.constraint.Guideline
       android:layout_width="wrap_content"
       android:layout_height="0dp"
       android:id="@+id/guideline2"
       app:layout_constraintGuide_percent="0.5"
       android:orientation="horizontal"/>
</android.support.constraint.ConstraintLayout> 

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.