Я пытаюсь заполнить относительный макет 4 изображениями одинакового размера. Для этого я пытаюсь получить размер макета, а затем настроить размер изображений, разделив размер макета на 4. Однако я не получаю размер макета должным образом; он продолжает возвращать 0 (как высоту, так и ширину). Вот что у меня есть на данный момент:
@Override
protected void onResume() {
super.onResume();
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
layoutHeight = mainLayout.getMeasuredHeight();
layoutWidth = mainLayout.getMeasuredWidth();
Log.d("Layout dimensions", layoutHeight + " " + layoutWidth);
}
Насколько я понимаю, onResume () будет запускаться после onCreate ()? Изначально у меня был этот раздел кода в onCreate (), и он там не работал, но он также не работает в onResume ().
Я тоже пробовал менять строчки
layoutHeight = mainLayout.getMeasuredHeight();
layoutWidth = mainLayout.getMeasuredWidth();
к
layoutHeight = mainLayout.getHeight();
layoutWidth = mainLayout.getWidth();
но это тоже не сработало.
Вот мой XML, на случай, если кто-то захочет это увидеть:
<?xml version = "1.0" encoding = "utf-8"?>
<RelativeLayout
android:id = "@+id/mainLayout"
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">
<ImageView
android:id = "@+id/bronzeView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
android:layout_alignParentTop = "true"
android:src = "@drawable/bronze" />
<ImageView
android:id = "@+id/silverView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentTop = "true"
android:layout_alignParentRight = "true"
android:src = "@drawable/silver" />
<ImageView
android:id = "@+id/goldView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentLeft = "true"
android:layout_alignParentBottom = "true"
android:src = "@drawable/gold" />
<!--<ImageView-->
<!--android:id = "@+id/roseGoldView"-->
<!--android:layout_width = "76dp"-->
<!--android:layout_height = "137dp"-->
<!--app:srcCompat = "@drawable/rosegold_small"-->
<!--tools:layout_editor_absoluteX = "336dp"-->
<!--tools:layout_editor_absoluteY = "557dp" />-->
<ImageView
android:id = "@+id/platinumView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignParentRight = "true"
android:layout_alignParentBottom = "true"
android:layout_marginRight = "0dp"
android:layout_marginBottom = "0dp"
android:src = "@drawable/platinum" />
</RelativeLayout>
Что я могу сделать, чтобы это исправить?
Спасибо.
Не могли бы вы показать свой xml, пожалуйста?
размеры любого представления можно измерить только и только тогда, когда представление полностью отображается на экране, но в onResume () представление просто создается, а не отображается.




Использовать этот -
mainLayout.post(new Runnable(){
layoutHeight = mainLayout.getMeasuredHeight();
layoutWidth = mainLayout.getMeasuredWidth();
}
I'm trying to fill a relative layout with 4 images of equal size. To do this I am trying to get the size of the layout then adjust the size of the images by dividing the layout size by 4.
Вы рассматривали возможность использования ConstraintLayout вместо RelativeLayout? Тогда вы могли бы делать все это в XML без необходимости выполнять какой-либо код.
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.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">
<ImageView
android:id = "@+id/image1"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintBottom_toTopOf = "@+id/image3"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintEnd_toStartOf = "@+id/image2"/>
<ImageView
android:id = "@+id/image2"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintTop_toTopOf = "parent"
app:layout_constraintBottom_toTopOf = "@+id/image4"
app:layout_constraintStart_toEndOf = "@+id/image1"
app:layout_constraintEnd_toEndOf = "parent"/>
<ImageView
android:id = "@+id/image3"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintTop_toBottomOf = "@+id/image1"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintStart_toStartOf = "parent"
app:layout_constraintEnd_toStartOf = "@+id/image4"/>
<ImageView
android:id = "@+id/image4"
android:layout_width = "0dp"
android:layout_height = "0dp"
app:layout_constraintTop_toBottomOf = "@+id/image2"
app:layout_constraintBottom_toBottomOf = "parent"
app:layout_constraintStart_toEndOf = "@+id/image3"
app:layout_constraintEnd_toEndOf = "parent"/>
</android.support.constraint.ConstraintLayout>
Вы должны использовать
ViewTreeObserverили получить размеры методомrun()с помощьюlayout.post()...