首页 > 解决方案 > Android base layout with buttons to use in multiple activities

问题描述

I'm developing an Android app where many activities need to have 4 buttons in their layout, one in each corner of the screen. The behavior of these buttons is always the same, regardless the current activity (for example, a sound off/sound on toggle button, a Help button, etc). I've designed a base_layout with the top right and top left buttons, and used the tag in the activities' layout. I've also created a BaseActivity class that has the behavior of the buttons, so every activity that includes them inherits from this class. So far it works okay, but if I add one of the buttons at the bottom of the screen, this layout overlaps with each activity's layout, so I only see the base_layout and not the activity layout.

Here's the base_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ImageButton
    android:id="@+id/btn_atras"
    android:src="@drawable/flecha_atras"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:background="@android:color/transparent"
   android:contentDescription="Atrás"/>

<ToggleButton
    android:id="@+id/btn_sonido"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:background="@android:color/transparent"
    android:button="@drawable/sonido_selector"
    android:checked="true"
    android:textOff=""
    android:textOn="" />

<ImageButton
    android:id="@+id/btn_ayuda"
    android:src="@drawable/ayuda"
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:background="@android:color/transparent"
    android:contentDescription="Ayuda"/>

</RelativeLayout>

And here is part of the layout from an activity that uses it:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_blue_light">

<include
    android:id="@+id/bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    layout="@layout/base_layout" />

<TextView
    android:id="@+id/textView"
    style="@style/TextoTitulo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/titulo_menu" />

<!--more stuff here that I'm cutting because it's irrelevant to the question--->
</LinearLayout>

What is the best way to achieve the 4 global buttons on each corner of the screen in every activity?

标签: androidandroid-layoutinclude

解决方案


您可以使用ConstraintLayoutmerge标签。

base_layout.xml 看起来像

<merge 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="wrap_content"
    tools:parentTag="android.support.constraint.ConstraintLayout">
    .
    .
    .

并且活动 xml 想要类似的东西

<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="wrap_content">

    <include
        android:id="@+id/bar"
        layout="@layout/base_layout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintEnd_toStartOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />
    .
    .
    .

推荐阅读