Подтвердить что ты не робот

Как добавить нижнее меню в Android-активность

Я пытаюсь добавить еще одну панель действий или меню в нижней части моего макета активности и сохранить верхнюю панель действий.

Что-то вроде этого:

Мой макет

Вот мой макет:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.yasser.version6.PublierActivity">


<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />



<RelativeLayout
     android:layout_width="fill_parent"
     android:layout_height="match_parent"
     >


    <LinearLayout
        android:layout_width="fill_parent"
        android:id="@+id/user"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1">


        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="center"
            android:layout_margin="4dp"
            android:id="@+id/profil_image"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="218dp"
            android:layout_marginEnd="218dp" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine|textCapSentences"
            android:ems="10"
            android:background="#00000000"
            android:layout_margin="4dp"
            android:hint="Écriver quelque chose..."
            android:id="@+id/publication_text"
            android:maxLength="150"
            android:textSize="15dp"
            android:maxLines="3"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true">
            <requestFocus />
        </EditText>


    </LinearLayout>




    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="@dimen/image_size"
        android:id="@+id/publication_image"
        android:layout_weight="1"
        android:padding="0dp"
        android:background="@drawable/default_image"
        android:layout_marginTop="5dp"
        android:scaleType="fitXY"
        android:layout_below="@+id/user" />

</RelativeLayout>

Я попытался добавить пользовательский RelativeLayout, который выглядит почти как панель действий, но панель инструментов поднимается и все испортит.

4b9b3361

Ответ 1

Создание пользовательской нижней панели инструментов

Я уже создал простое приложение, которое должно продемонстрировать вам, как начать

6ZBkF.pnggf81d.png

Создание пользовательской ViewGroup

Вот мой макет activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0dp"
    tools:context="com.example.piotr.myapplication.MainActivity">

    <LinearLayout
        android:id="@+id/show_pdf"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:background="@color/primary_material_light"
        android:orientation="horizontal">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_cut_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_copy_mtrl_am_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_paste_mtrl_am_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_share_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/>

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"/>
    </LinearLayout>

    <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="75dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Name"/>
</RelativeLayout>

Как вы можете видеть, мой родительский ViewGroup - это RelativeLayout, который просто позволяет мне создать представление внизу экрана.

Обратите внимание, что я установил отладку макета до нуля (я думаю: установка разметки макета на ноль здесь не требуется, тот же эффект). Если вы измените его, панель инструментов не будет использовать полную ширину, и она не будет придерживаться нижней части экрана.

Затем я добавил линейную компоновку с жестко запрограммированной высотой, которая:

          android:layout_height="40dp"

Я хотел, чтобы моя нижняя панель инструментов имела полную доступную ширину, поэтому я установил ее как match_parent.

Затем я добавил несколько просмотров ImageButton с изображениями из библиотеки Android.

Здесь у вас есть две возможности:

  • Если вы действительно хотите, чтобы панель инструментов, как в приведенном выше примере, просто удаляла в каждой строке ImageButton эту строку:

          android:layout_weight="1"
    

После удаления весов и некоторых кнопок вы получите представление, похожее на ожидаемое:

JSli8.png

  • если вы хотите взять полную ширину и сделать каждую кнопку с таким же размером в проекте weight, как в этом примере.

Теперь откройте мой AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.piotr.myapplication"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:windowSoftInputMode="stateVisible|adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

В том файле, который я добавил, поскольку вы видите только одну дополнительную строку:

         android:windowSoftInputMode="stateVisible|adjustResize">

чтобы клавиатура устройства не скрывала мою пользовательскую нижнюю панель инструментов.

Если у вас есть какие-либо вопросы, пожалуйста, свяжитесь с нами. Я бы ответил на них как можно быстрее.

Надеюсь, что это поможет

Ответ 2

Насколько я знаю, нет возможности полностью перемещать ActionBar на дно. Но все же можно отобразить несколько элементов внизу. для этого вам нужно это сделать:

  • Просто добавьте android:uiOptions="splitActionBarWhenNarrow" в тэг activity в AndroidManifest.xml, как это...

    <activity android:name=".MainActivity" android:uiOptions="splitActionBarWhenNarrow">

Здесь вы можете прочитать здесь и здесь

Надеюсь, это поможет.:)

Ответ 3

Я создал пользовательскую нижнюю панель с точной высотой? attr/actionBarSize и `layout_alignParentBottom = "true", чтобы поместить ее внизу.

<LinearLayout
    android:id="@+id/bottombar"
    android:layout_width="fill_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentBottom="true"
    android:background="#FFFFFF"
    android:weightSum="30"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:src="@drawable/iconcamera"/>
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:src="@drawable/shape"/>
</LinearLayout>`

Теперь goto Manifest.xml и в вашем теге активности поставьте

android:windowSoftInputMode="adjustResize"

Это изменит размер макета при открытии клавиатуры.

Ответ 4

Это панель действий с разделом. Он доступен для мобильных устройств с шириной экрана 400dp < Чтобы включить панель действия split, просто добавьте uiOptions = "splitActionBarWhenNarrow" к вашему элементу или манифеста.

Изменить: второе изображение действительно не является панелью действий с разделом. Это просто кнопки с атрибутом buttonBarButtonStyle. Посмотрите на это здесь.

Ответ 5

Вы можете просто добавить панель инструментов и установить ее снизу и просто создать собственный собственный макет.

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="#333333" app:popupTheme="@style/AppTheme.PopupOverlay"
        android:layout_alignParentBottom="true"

        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="left">

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/folder"
                android:id="@+id/pdfViewer"/>

        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_gravity="center"
            >
            <Button
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:background="@drawable/cameranew"
                android:id="@+id/btn"
                />
        </LinearLayout>
       </android.support.v7.widget.Toolbar>