Android studio 之 操作栏与fragment
操作栏(ActionBar) 和Fragment是 Android 3.0新引入的界面控件,一定程度上是为了适应Android平板电脑等大屏幕设备界面设计需要而产生的。在Android 4.0系统中得到了进一步的发展,可以良好的支持不同屏幕尺寸的设备,并可以根据屏幕大小的不同改变显示内容。
操作栏
首先,在drawable文件夹下导入选择好的操作栏属性图片
在values文件夹下的字符资源文件strings.xml里面添加字符设置
在layout文件夹里新建fragment_layout.xml为主界面和操作栏图标文件chart_view.xml
在fragment_layout.xml文件中使用线性布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="30dp"
android:text="操作栏测试" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@drawable/ic_launcher_background" />
</LinearLayout>
在chart_view.xml中使用线性布局,居中显示,添加图片和文本控件,将drawable文件夹中的chart_icon.png文件导入。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="@drawable/chart_icon" />
<EditText
android:id="@+id/editText"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout>
在menu文件夹里更改原有的菜单文件menu_main.xml文件,改名为fragement_main.xml,并在原有代码上添加多个一级菜单选项item,分别为记录、邮件、设置
app:showAsAction中有三个可选项:
1、always:总是显示在界面上
2、never:不显示在界面上,只让出现在右边的三个点中
3、ifRoom:如果有位置才显示,不然就出现在右边的三个点中
在记录item中添加记录的图标文件chart_icon.png、针对图标文件编写的chart_view.xml文件。之后的图标操作类似。
<menu 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"
tools:context="com.example.menutouch.MainActivity">
<!-- 在item中添加图片代码android:icon="@drawable/chart_icon"
android:actionLayout="@layout/chart_view"
将 app:showAsAction="never"中的never改成"ifRoom|withText" -->
<item
android:id="@+id/chart"
android:orderInCategory="100"
android:title="@string/action_chart"
a