Текущее фото макета при тестировании
На разных устройствах макет резко меняется. Я искал ответы в Google, но ничто не дало ясности между тем, что является правильным способом делать что-то, а что нет. Что может быть сделано?
Примечание. Существует двадцать идентичных копий последнего представления изображения, которые не включены в приведенный ниже код. Они предназначены для перемещения для графики.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android"
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"
tools:context = ".MainActivity">
<com.example.castbuddyv1.FFVisuals
android:id = "@+id/FFVisuals"
android:layout_width = "389dp"
android:layout_height = "781dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintHorizontal_bias = "0.4"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toBottomOf = "@+id/rawdata" />
<Button
android:id = "@+id/right2"
android:layout_width = "59dp"
android:layout_height = "48dp"
android:layout_marginStart = "84dp"
android:layout_marginLeft = "84dp"
android:layout_marginTop = "658dp"
android:layout_marginBottom = "29dp"
app:icon = "@drawable/abc_vector_test"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toStartOf = "@+id/onoff"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintVertical_bias = "0.39" />
<ToggleButton
android:id = "@+id/onoff"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginStart = "32dp"
android:layout_marginLeft = "32dp"
android:layout_marginTop = "700dp"
android:layout_marginEnd = "32dp"
android:layout_marginRight = "32dp"
android:layout_marginBottom = "32dp"
android:rotation = "0"
android:textOff = "Motors On"
android:textOn = "Motors Off"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintHorizontal_bias = "0.497"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintVertical_bias = "0.922" />
<Button
android:id = "@+id/right"
android:layout_width = "59dp"
android:layout_height = "48dp"
android:layout_marginTop = "658dp"
android:layout_marginEnd = "51dp"
android:layout_marginRight = "51dp"
android:layout_marginBottom = "29dp"
android:rotation = "180"
app:icon = "@drawable/abc_vector_test"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintHorizontal_bias = "0.0"
app:layout_constraintStart_toEndOf = "@+id/onoff"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintVertical_bias = "1.0" />
<TextView
android:id = "@+id/statusdisplay"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginStart = "166dp"
android:layout_marginLeft = "166dp"
android:layout_marginTop = "595dp"
android:layout_marginEnd = "183dp"
android:layout_marginRight = "183dp"
android:layout_marginBottom = "44dp"
android:text = "Status:"
app:layout_constraintBottom_toTopOf = "@+id/onoff"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "parent" />
<TextView
android:id = "@+id/depth"
android:layout_width = "80dp"
android:layout_height = "31dp"
android:layout_marginEnd = "49dp"
android:layout_marginRight = "49dp"
android:text = "Depth:"
app:layout_constraintBottom_toBottomOf = "@+id/rawdata"
app:layout_constraintEnd_toStartOf = "@+id/connect"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "@+id/connect"
app:layout_constraintVertical_bias = "1.0" />
<Switch
android:id = "@+id/rawdata"
android:layout_width = "92dp"
android:layout_height = "34dp"
android:layout_marginStart = "16dp"
android:layout_marginLeft = "16dp"
android:layout_marginBottom = "692dp"
android:text = "Raw Data"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toEndOf = "@+id/connect"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintVertical_bias = "0.0" />
<Button
android:id = "@+id/connect"
android:layout_width = "110dp"
android:layout_height = "38dp"
android:layout_marginStart = "16dp"
android:layout_marginLeft = "16dp"
android:layout_marginEnd = "16dp"
android:layout_marginRight = "16dp"
android:text = "@string/connect"
app:layout_constraintBottom_toBottomOf = "@+id/rawdata"
app:layout_constraintEnd_toStartOf = "@+id/rawdata"
app:layout_constraintStart_toEndOf = "@+id/depth"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintVertical_bias = "1.0" />
<ImageView
android:id = "@+id/fish"
android:layout_width = "57dp"
android:layout_height = "33dp"
android:layout_marginStart = "16dp"
android:layout_marginLeft = "16dp"
android:layout_marginEnd = "320dp"
android:layout_marginRight = "320dp"
android:layout_marginBottom = "87dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:srcCompat = "@drawable/kisspng_computer_icons_symbol_clip_art_5b2bf54c62bee1_4646889015296075004045
</androidx.constraintlayout.widget.ConstraintLayout>
Конечно, фото выше.
Я не уверен, что это за часть:
<com.example.castbuddyv1.FFVisuals
android:id = "@+id/FFVisuals"
android:layout_width = "389dp"
android:layout_height = "781dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintHorizontal_bias = "0.4"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toBottomOf = "@+id/rawdata" />
Я новичок, я не знаю, поможет ли это вам, но, допустим, я хочу разработать ваш интерфейс, для себя я сделаю это:
Убедитесь, что интерфейс отображается вертикально, добавив приведенный ниже код в файл AndroidManifest.xml
:
android:screenOrientation = "sensorPortrait"
<category android:name = "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Размещение вертикальных и горизонтальных направляющих в макете ограничения:
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/v_guideline1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "vertical"
app:layout_constraintGuide_begin = "20dp" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/h_guideline1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
app:layout_constraintGuide_begin = "20dp" />
Замените (app:layout_constraintGuide_begin = "20dp")
на (app:layout_constraintGuide_percent = "x%")
, чтобы расположить его в зависимости от процента.
Например: 30 % для первой вертикальной направляющей и 50 % для первой. горизонтальная направляющая:
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/guideline1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "vertical"
app:layout_constraintGuide_percent = "0.3" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/h_guideline1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
app:layout_constraintGuide_percent = "0.5" />
Это то, что я делаю, чтобы убедиться, что каждый виджет появляется в нужном мне месте.
Вот как это выглядит в студии Android:
вот как это выглядит в: Pixel XL 1440 × 2560: 560 точек на дюйм: Пиксель XL
вот как это выглядит в: Nexus 9 2048×1536:xhdpi24: Нексус 9
вы можете видеть, что размер экрана изменился, но виджеты остаются в том же положении
это xml для этого интерфейса:
<?xml version = "1.0" encoding = "utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
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"
tools:context = ".MainActivity">
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/h_0"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
app:layout_constraintGuide_percent = "0.03" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/h_1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
app:layout_constraintGuide_percent = "0.1" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/h_2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
app:layout_constraintGuide_percent = "0.8" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/h_3"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
app:layout_constraintGuide_percent = "0.9" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/h_4"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "horizontal"
app:layout_constraintGuide_percent = "0.97" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/v_0"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "vertical"
app:layout_constraintGuide_percent = "0.2" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/v_1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "vertical"
app:layout_constraintGuide_percent = "0.35" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/v_2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "vertical"
app:layout_constraintGuide_percent = "0.65" />
<androidx.constraintlayout.widget.Guideline
android:id = "@+id/v_3"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:orientation = "vertical"
app:layout_constraintGuide_percent = "0.8" />
<TextView
android:id = "@+id/depth"
android:layout_width = "0dp"
android:layout_height = "0dp"
android:text = "Depth:"
app:layout_constraintTop_toTopOf = "@+id/h_0"
app:layout_constraintBottom_toTopOf = "@+id/h_1"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintEnd_toStartOf = "@+id/v_0"
/>
<Button
android:id = "@+id/connect"
android:layout_width = "0dp"
android:layout_height = "0dp"
android:text = "@string/connect"
app:layout_constraintTop_toTopOf = "@+id/h_0"
app:layout_constraintBottom_toTopOf = "@+id/h_1"
app:layout_constraintStart_toEndOf = "@+id/v_1"
app:layout_constraintEnd_toStartOf = "@+id/v_2"
/>
<Switch
android:id = "@+id/rawdata"
android:layout_width = "0dp"
android:layout_height = "0dp"
android:text = "Raw Data"
app:layout_constraintTop_toTopOf = "@+id/h_0"
app:layout_constraintBottom_toTopOf = "@+id/h_1"
app:layout_constraintStart_toEndOf = "@+id/v_3"
app:layout_constraintEnd_toEndOf = "parent"
/>
<Button
android:id = "@+id/right2"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:icon = "@drawable/abc_vector_test"
app:layout_constraintTop_toBottomOf = "@+id/h_3"
app:layout_constraintBottom_toTopOf = "@+id/h_4"
app:layout_constraintStart_toStartOf = "@+id/v_0"
app:layout_constraintEnd_toStartOf = "@+id/v_1"
/>
<ToggleButton
android:id = "@+id/onoff"
android:layout_width = "0dp"
android:layout_height = "0dp"
android:rotation = "0"
android:textOff = "Motors On"
android:textOn = "Motors Off"
app:layout_constraintTop_toBottomOf = "@+id/h_3"
app:layout_constraintBottom_toTopOf = "@+id/h_4"
app:layout_constraintStart_toEndOf = "@+id/v_1"
app:layout_constraintEnd_toStartOf = "@+id/v_2"
/>
<Button
android:id = "@+id/right"
android:layout_width = "0dp"
android:layout_height = "0dp"
android:rotation = "180"
app:icon = "@drawable/abc_vector_test"
app:layout_constraintTop_toBottomOf = "@+id/h_3"
app:layout_constraintBottom_toTopOf = "@+id/h_4"
app:layout_constraintStart_toEndOf = "@+id/v_2"
app:layout_constraintEnd_toStartOf = "@+id/v_3"
/>
<TextView
android:id = "@+id/statusdisplay"
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:text = "Status:"
app:layout_constraintTop_toTopOf = "@+id/h_2"
app:layout_constraintStart_toStartOf = "@+id/v_1"
app:layout_constraintEnd_toStartOf = "@+id/v_2"
/>
<ImageView
android:id = "@+id/fish"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintTop_toTopOf = "@+id/h_2"
app:layout_constraintBottom_toTopOf = "@+id/h_3"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintEnd_toStartOf = "@+id/v_0"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
обратите внимание, что я установил ширину и высоту виджетов на 0dp:
android:layout_width = "0dp"
android:layout_height = "0dp"
Поскольку рекомендации задаются в процентах, виджеты будут растягиваться и сжиматься в соответствии с размером экрана.
вы можете сделать виджеты фиксированными по размеру, назначив значения dp, например:
android:layout_width = "70dp"
android:layout_height = "40dp"
вот и все, что у меня получилось, надеюсь мое объяснение понятно и полезно.
С Рождеством!
Вы можете узнать ConstrainLayout
от здесь
Я попытался выяснить проблему с ограничением, примененным в вашем коде. Я надеюсь, что это поможет вам, пожалуйста, проверьте этот код:
<?xml version = "1.0" encoding = "utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res-auto"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
xmlns:tools = "http://schemas.android.com/tools"
tools:context = ".MainActivity">
<com.example.castbuddyv1.FFVisuals
android:id = "@+id/FFVisuals"
android:layout_width = "389dp"
android:layout_height = "781dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintHorizontal_bias = "0.4"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toBottomOf = "@+id/rawdata" />
<Button
android:id = "@+id/right2"
android:layout_width = "59dp"
android:layout_height = "48dp"
android:layout_marginBottom = "29dp"
app:icon = "@drawable/abc_vector_test"
app:layout_constraintBaseline_toBaselineOf = "@id/onoff"
app:layout_constraintEnd_toStartOf = "@+id/onoff" />
<ToggleButton
android:id = "@+id/onoff"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginBottom = "32dp"
android:rotation = "0"
android:textOff = "Motors On"
android:textOn = "Motors Off"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent" />
<Button
android:id = "@+id/right"
android:layout_width = "59dp"
android:layout_height = "48dp"
android:layout_marginBottom = "29dp"
android:rotation = "180"
app:icon = "@drawable/abc_vector_test"
app:layout_constraintBaseline_toBaselineOf = "@id/onoff"
app:layout_constraintStart_toEndOf = "@+id/onoff" />
<TextView
android:id = "@+id/statusdisplay"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_marginBottom = "44dp"
android:text = "Status:"
app:layout_constraintBottom_toTopOf = "@+id/onoff"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent" />
<TextView
android:id = "@+id/depth"
android:layout_width = "80dp"
android:layout_height = "31dp"
android:layout_marginStart = "16dp"
android:text = "Depth:"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "@+id/connect" />
<Switch
android:id = "@+id/rawdata"
android:layout_width = "92dp"
android:layout_height = "34dp"
android:layout_marginEnd = "16dp"
android:text = "Raw Data"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintTop_toTopOf = "parent" />
<Button
android:id = "@+id/connect"
android:layout_width = "110dp"
android:layout_height = "38dp"
android:layout_marginStart = "16dp"
android:layout_marginLeft = "16dp"
android:layout_marginEnd = "16dp"
android:layout_marginRight = "16dp"
android:text = "Connect"
app:layout_constraintEnd_toEndOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintTop_toTopOf = "parent" />
<ImageView
android:id = "@+id/fish"
android:layout_width = "57dp"
android:layout_height = "33dp"
android:layout_marginStart = "16dp"
android:layout_marginBottom = "87dp"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:srcCompat = "@mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>
Пожалуйста, добавьте несколько скриншотов, чтобы мы могли понять, что вы имеете в виду. Стандартной практикой является использование разных файлов макета для разных размеров экрана.