Ответ 1
Создание пользовательской нижней панели инструментов
Я уже создал простое приложение, которое должно продемонстрировать вам, как начать
Создание пользовательской 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"
После удаления весов и некоторых кнопок вы получите представление, похожее на ожидаемое:
- если вы хотите взять полную ширину и сделать каждую кнопку с таким же размером в проекте
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">
чтобы клавиатура устройства не скрывала мою пользовательскую нижнюю панель инструментов.
Если у вас есть какие-либо вопросы, пожалуйста, свяжитесь с нами. Я бы ответил на них как можно быстрее.
Надеюсь, что это поможет