У меня есть 3 файла макета в папке res / layout:
activity_main.xml
section_1.xml
section_2.xml
Оба section_1.xml & section_2.xml содержат ConstraintLayout с парой ImageViews внутри них.
Однако в section_2.xml все ImageView имеют фиксированное ограничение x / y, в то время как в section_1.xml все ImageView имеют атрибут constraintHorizont_bias & constraintVertical_bias.
Я хотел скопировать все представления из одного макета (section_1 / section_2) к моему основному макету (activity_main).
Копирование представлений из section_2 в activity_main прошло нормально ... Все вложенные ImageViews отображались на экране с правильным расположением, но копирование представлений из section_1 в activity_main не работало должным образом.Представления действительно отображаются, но все они переместились в верхний левый угол экрана.
Кроме того, при регистрации местоположений всех представлений значения x & y равны 0 в section_1 (тогда как в section_2 ониотобразите правильные значения).
Итак, мой вопрос:
Как я могу успешно скопировать (изображение) виды с горизонтальным / вертикальным смещением в другой макет?
Я попытался скопировать Views с фиксированным ограничением x / y, которое работало нормально.
Я также посмотрел на методы View, чтобы увидеть, существует ли такой метод, как 'getVerticalBias() 'или' getHor horizontalBias () ', но безуспешно.
Вот код, который относится к моей проблеме:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadSections();
}
private void loadSections(){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Log.w("SIZE", "x: " + size.x + ", y: " + size.y );
ViewGroup main = findViewById(R.id.main_constraint_layout);
LayoutInflater inflater = LayoutInflater.from(this);
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.section_1, null);
viewGroup.measure(size.x, size.y);
main.addView(viewGroup);
viewGroup.post( () -> {
Log.w("VIEWGROUP SIZE", viewGroup.getWidth() + ", " + viewGroup.getHeight());
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View view = viewGroup.getChildAt(i);
Log.w("CHILD LOCATION", "x: " + toDp(view.getX()) + ", y: " + toDp(view.getY()) );
}
});
}
private float toDp(float px){
float density = getResources().getDisplayMetrics().density;
return px / density;
}
}
вот содержимое section_1.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/rock3"
android:layout_width="30dp"
android:layout_height="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.75"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.75"
app:srcCompat="@drawable/rock_pile" />
<ImageView
android:id="@+id/rock4"
android:layout_width="30dp"
android:layout_height="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1"
app:srcCompat="@drawable/rock_pile" />
</android.support.constraint.ConstraintLayout>
и вот содержание section_2.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/diamond_1"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/diamond" />
<ImageView
android:id="@+id/diamond_3"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginStart="476dp"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/diamond" />
</android.support.constraint.ConstraintLayout>
Любая помощь приветствуется.