为APP添加navgraph,侧边栏的菜单,图标等

使用到的库
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
master
ThankVinci 4 years ago
parent 612d0fa7b8
commit 2dd545cada

@ -33,6 +33,8 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

@ -4,9 +4,9 @@
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:roundIcon="@mipmap/icon"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.AppCompat.Light.DarkActionBar">

@ -0,0 +1,11 @@
package com.thankvinci.CloudKey.Fragment;
import androidx.fragment.app.Fragment;
import com.thankvinci.CloudKey.R;
public class AboutFragment extends Fragment {
public AboutFragment(){
super(R.layout.about_fragment);
}
}

@ -0,0 +1,18 @@
package com.thankvinci.CloudKey.Fragment;
import android.os.Bundle;
import android.view.View;
import androidx.fragment.app.Fragment;
import com.thankvinci.CloudKey.R;
public class FileManageFragment extends Fragment {
public FileManageFragment(){
super(R.layout.filemanage_fragment);
}
@Override
public void onViewCreated(View view,Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}

@ -1,29 +1,43 @@
package com.thankvinci.CloudKey;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
import com.google.android.material.navigation.NavigationView;
import com.thankvinci.CloudKey.NetUtils.NetHandler;
import com.thankvinci.CloudKey.NetUtils.NetThread;
public class MainActivity extends AppCompatActivity {
private NavHostFragment navHostFragment;
private NavController navController;
private AppBarConfiguration appBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//声明一个Handler
Handler handler = new NetHandler(this);
TextView textView = findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread t = new Thread(new NetThread(handler));
t.start();
//加载NavGraph
navHostFragment = (NavHostFragment)getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
navController = navHostFragment.getNavController();
DrawerLayout drawerLayout = findViewById(R.id.drawLayout);
appBarConfiguration = new AppBarConfiguration.Builder(R.id.fileManageFragment).setDrawerLayout(drawerLayout).build();
NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
NavigationView navigationView = findViewById(R.id.nav_view);
NavigationUI.setupWithNavController(navigationView,navController);
}
});
@Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController,appBarConfiguration)||super.onSupportNavigateUp();
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Fragment.AboutFragment">
</androidx.constraintlayout.widget.ConstraintLayout>

@ -1,19 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.drawerlayout.widget.DrawerLayout 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/drawLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"/>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:id="@+id/text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/nav_menu"/>
</androidx.drawerlayout.widget.DrawerLayout>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".Fragment.FileManageFragment" >
</androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/fileManageFragment" android:title="文件"></item>
<item android:id="@+id/aboutFragment" android:title="关于我们"></item>
</menu>

Binary file not shown.

After

Width:  |  Height:  |  Size: 55 KiB

@ -0,0 +1,17 @@
<?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/nav_graph"
app:startDestination="@id/fileManageFragment">
<fragment
android:id="@+id/fileManageFragment"
android:name="com.thankvinci.CloudKey.Fragment.FileManageFragment"
android:label="filemanagefragment"
tools:layout="@layout/filemanage_fragment" />
<fragment
android:id="@+id/aboutFragment"
android:name="com.thankvinci.CloudKey.Fragment.AboutFragment"
android:label="about_fragment"
tools:layout="@layout/about_fragment" />
</navigation>

@ -123,3 +123,12 @@ private String queryPassword(PrintWriter writer, Connection conn, String filemd5
在这里,Web端print的是一个json型的字符串,所以Android端接受到这个字符串要进行解析,这里使用的是alibaba的fastjson库:implementation group: 'com.alibaba', name: 'fastjson', version: '1.2.75'
经过测试,成功Toast出数据库中对应md5码的pwd
**2021/6/8**
为APP添加navgraph,侧边栏的菜单,图标等
使用到的库
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'
Loading…
Cancel
Save