android - 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?
解决方案
您可以使用ConstraintLayout
和merge
标签。
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" />
.
.
.
推荐阅读
- python - 双击py文件时如何阻止它们运行?
- wordpress - 如何在 wordpress 插件中链接 css 和 js?
- pandas - Pandas 在括号内拆分/提取字符串,但有异常
- python - 如何在没有安装 pip 或 conda 的情况下读取任何 python 模块
- node.js - Koa 中的流式请求正文
- excel - 防止Excel中一系列列中的数据重复
- java - 如何使用注释正确地从 bean 中提取字段
- android - 如何在android片段中使用辅助构造函数分配后期初始化变量?
- python - 如何使用 pip3 安装
如果我已经有包裹,则离线 - chart.js - 半甜甜圈,带有指向进度 Chart.js 的箭头