创建自定义底部工具栏
我已经创建了一个简单的应用程序,它应该向您展示如何开始
创建自定义ViewGroup
这是我的activity_main.xml布局文件:
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">
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">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abc_ic_menu_cut_mtrl_alpha"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abc_ic_menu_copy_mtrl_am_alpha"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abc_ic_menu_paste_mtrl_am_alpha"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abc_ic_menu_share_mtrl_alpha"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abc_ic_menu_selectall_mtrl_alpha"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/abc_ic_menu_moreoverflow_mtrl_alpha"/>
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"/>
正如你可以看到我的父母ViewGroup是RelativeLayout,它只是允许我创建在屏幕底部的视图.
请注意,我将布局填充设置为零(我认为:此处将布局边距设置为零是不必要的,效果相同).如果您更改它,工具栏将不会使用全宽,它不会粘在屏幕的底部.
然后我添加了一个带有硬编码高度的线性布局,它是:
android:layout_height="40dp"
我想要它,我的底部工具栏将采用完全可用的宽度,所以我将其设置为match_parent.
接下来,我ImageButton使用Android库中的图像添加了一些视图.
那里有两种可能性:
如果你真的想拥有一个像上面例子中的工具栏,只需在每一个ImageButton视图中删除这一行:
android:layout_weight="1"
删除重量和一些按钮后,您将获得与预期非常相似的视图:
如果你想获取整个宽度并使你的项目中使用相同大小的每个按钮,weight就像在我的例子中一样.
现在让我们转到我的AndroidManifest.xml
xmlns:android="http://schemas.android.com/apk/res/android">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:name=".MainActivity"
android:windowSoftInputMode="stateVisible|adjustResize">
在我添加的文件中,您只能看到一行:
android:windowSoftInputMode="stateVisible|adjustResize">
确保设备键盘不会隐藏我的自定义底部工具栏.