Android基础布局,带有可在多个活动中使用的按钮

时间:2018-06-18 22:47:55

标签: android android-layout include

我正在开发一款Android应用,其中许多活动需要在其布局中设置4个按钮,屏幕的每个角落都有一个按钮。无论当前活动如何(例如,关闭/声音切换按钮,帮助按钮等),这些按钮的行为始终相同。我已经设计了一个带有右上角和左上角按钮的base_layout,并在活动中使用了标签'布局。我还创建了一个具有按钮行为的BaseActivity类,因此包含它们的每个活动都继承自此类。到目前为止它工作正常,但如果我在屏幕底部添加一个按钮,这个布局与每个活动的布局重叠,所以我只看到base_layout而不是活动布局。

这里是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>

以下是使用它的活动的布局的一部分:

<?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>

在每项活动中,在屏幕的每个角落实现4个全局按钮的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用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" />
    .
    .
    .