Android studio 之 操作栏与fragment

Android studio 之 操作栏与fragment

操作栏(ActionBar) 和Fragment是 Android 3.0新引入的界面控件,一定程度上是为了适应Android平板电脑等大屏幕设备界面设计需要而产生的。在Android 4.0系统中得到了进一步的发展,可以良好的支持不同屏幕尺寸的设备,并可以根据屏幕大小的不同改变显示内容。

操作栏

首先,在drawable文件夹下导入选择好的操作栏属性图片
图1图2图3
在values文件夹下的字符资源文件strings.xml里面添加字符设置
图4

在layout文件夹里新建fragment_layout.xml为主界面和操作栏图标文件chart_view.xml
图5
在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>

图6

在menu文件夹里更改原有的菜单文件menu_main.xml文件,改名为fragement_main.xml,并在原有代码上添加多个一级菜单选项item,分别为记录、邮件、设置
图7
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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值