Проблема с размещением кнопок(XML)
Никак не могу сделать так, чтобы "ImageButton" были отцентрированы по горизонтали и по центру. Помогите пожалуйста решить эту проблему(Они должны находится рядом). Просто, как отцентрировать одну кнопку я знаю, а вот как несколько нет.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<Button
android:id="@+id/button_administation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Экран администратора"
android:textColor="#0d0b03"
android:textSize="20dp"
android:layout_alignParentTop="true"
android:layout_toEndOf="@+id/button_service" />
<Button
android:id="@+id/button_service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Сервис"
android:textColor="#0d0b03" />
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/qr_button"
android:layout_width="640px"
android:layout_height="640px"
android:src="@drawable/qr_scan_icon"
android:layout_centerVertical="true"
android:gravity = "center"
android:scaleType="centerCrop"
android:contentDescription="@null"/>
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/lupa_button"
android:layout_width="640px"
android:layout_height="640px"
android:contentDescription="@null"
android:gravity="center"
android:scaleType="centerCrop"
android:layout_centerVertical="true"
android:src="@drawable/qr_scan_icon"
android:text="Сканировать QR"
android:textSize="12dp" />
<Button
android:id="@+id/genQR_button"
android:layout_width="640px"
android:layout_height="160px"
android:visibility="invisible"
android:layout_centerHorizontal="true"
android:layout_below="@id/qr_button"
android:gravity = "center"
android:textSize="12dp"
android:text="сгенерировать QR"/>
</RelativeLayout>
</LinearLayout>
P.S. ID кнопок "lupa_button" и "qr_button"
Ответы (3 шт):
Вашу задачу можно решить при помощи одного linear layout в котором будут расположены ваши кнопки:
<LinearLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity = "center"
android:orientation="horizontal"
android:weightSum="20">
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/qr_button"
android:layout_width="0dp"
android:layout_height="640px"
android:src="@drawable/qr_scan_icon"
android:layout_centerVertical="true"
android:gravity = "center"
android:layout_weight="10"
android:scaleType="centerCrop"
android:contentDescription="@null"/>
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/lupa_button"
android:layout_width="0dp"
android:layout_height="640px"
android:contentDescription="@null"
android:gravity="center"
android:layout_weight="10"
android:scaleType="centerCrop"
android:layout_centerVertical="true"
android:src="@drawable/qr_scan_icon"
android:text="Сканировать QR"
android:textSize="12dp" />
</LinearLayout>
То есть дальше вы центрируете один элемент - LinearLayout в котором будут две кнопки размещены друг за другом. Ориентация родительского макета будет размещать вложенные макеты горизонтально. Ширина каждого "ребенка" будут регулироваться при помощи - android:layout_weight. В данном примере размеры кнопок одинаковые и составляют по 50% от родительского. Родительский макет можно тоже настроить по размерам так как вам нужно.
Вы можете поместить ваши кнопки в блок (Например LinearLayout) и отцентровать этот блок.
P.S. не используйте px (лучше dp) в разметке, это будет плохо смотрится на разных устройствах
<LinearLayout
android:id="@+id/LinearLayoutWithButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/qr_button"
android:layout_width="640dp"
android:layout_height="640px"
android:contentDescription="@null"
android:scaleType="centerCrop"
android:src="@drawable/qr_scan_icon" />
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/lupa_button"
android:layout_width="640dp"
android:layout_height="640px"
android:layout_marginEnd="parent"
android:contentDescription="@null"
android:gravity="center"
android:scaleType="centerCrop"
android:src="@drawable/qr_scan_icon"
android:text="Сканировать QR"
android:textSize="12dp" />
</LinearLayout>
Используйте Constraint Layout
<?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"
android:background="@color/white">
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button_2"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toEndOf="@id/button_1"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Ключевым здесь является строчка
app:layout_constraintHorizontal_chainStyle="packed"