因为某些原因,打算学习安卓开发,记录一下每一步。
Bottom Navigation Activity
首先开发工具是as3,新建一个bottom navigation activity,因为底部导航栏是比较常用的嘛,这里就直接建一个项目。
文件结构
androidmainfest.xml
这个是android的主配置文件,采用XML描述语言。
AndroidManifest.xml配置文件的根元素,必须包含一个
android:versionCode是给设备程序识别版本用的,必须是一个整数值代表app更新过多少次;而android:versionName则是给用户查看版本用的,需要具备一定的可读性,比如“1.0.0”这样的。
下面是一份官方的AndroidManifest.xml,格式是这样,具体解析就不赘述了。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.sample.teapot"
          android:versionCode="1"
          android:versionName="1.0.0.1" >
  <uses-feature android:glEsVersion="0x00020000"></uses-feature>
  <application
      android:allowBackup="false"
      android:fullBackupContent="false"
      android:supportsRtl="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme"
      android:name="com.sample.teapot.TeapotApplication"
      >
    <!-- Our activity is the built-in NativeActivity framework class.
         This will take care of integrating with our NDK code. -->
    <activity android:name="com.sample.teapot.TeapotNativeActivity"
              android:label="@string/app_name"
              android:configChanges="orientation|keyboardHidden">
      <!-- Tell NativeActivity the name of our .so -->
      <meta-data android:name="android.app.lib_name"
                 android:value="TeapotNativeActivity" />
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
  </application>
</manifest>
java
java文件夹里有dashboard,home,notifications三个文件夹和一个MainActivity.三个文件夹分别是对应的底部三个导航栏。每个文件夹下有两个文件,一个是用来承载控件的fragment,另一个是与之对应的viewModel。fragment用来显示ui界面,而viewmodel则是给ui界面提供数据,view里的每一个控件在viewmodel里都有一个对应的数据对象,如果要更新view上的ui界面,只需要更新viewmodel里与之对应的对象即可。
res
menu
menu文件夹下的bottom_nav_menu.xml操控底部导航栏图标。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />
    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />
    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />
    <item
        android:id="@+id/navigation_me"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_me" />
</menu>
这里我可以自定义一个底部item,这里的@string/title_home是在res->values->strings.xml里定义。然后在java->ui文件夹下新建一个me的Package,在Package里右键new->Fragment->Fragment(with ViewMoudle)。
然后修改MainActivity里的代码。
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications,R.id.navigation_me)
                .build();
layout
layout下是每一个的布局文件。
navigation
相应修改此处的mobile_navigation.xml.
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
    android:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home"> //默认启动页
    <fragment
        android:id="@+id/navigation_home"
        android:name="com.chenxiyuan.youbang.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />
    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.chenxiyuan.youbang.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />
    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.chenxiyuan.youbang.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
    <fragment
        android:id="@+id/navigation_me"
        android:name="com.chenxiyuan.youbang.ui.me.MeFragment"
        android:label="@string/title_me"
        tools:layout="@layout/me_fragment" />
</navigation>
webview组件
先简单用一下webview组件。
在mainfest里添加网络权限
<uses-permission android:name="android.permission.INTERNET" />
在layout里对应的布局文件里,Palette->Widgets->Webview,添加组件。
<WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
这里我直接在HomeFragment里调用webview.
public class HomeFragment extends Fragment {
    private HomeViewModel homeViewModel;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        WebView myWebView = (WebView) root.findViewById(R.id.webview);
        myWebView.loadUrl("http://www.baidu.com");
        return root;
    }
}
刚开始报错,在网上搜了一下,解决方法是在manifest中application节点添加:
android:usesCleartextTraffic="true"
然后运行虚拟机,成功访问网页。
本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!