主界面布局
添加依赖
因为要使用到 com.google.android.material.tabs.TabLayout 所以前往 build.gradle 中添加
implementation "com.google.android.material:material:1.1.0"
布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/vp2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="50dp">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="搜索" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新闻" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我的" />
</com.google.android.material.tabs.TabLayout>
</LinearLayout>
主页Java代码
package com.example.goodrubish;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabLayout tl = findViewById(R.id.tablayout);
ViewPager2 viewPager2 = findViewById(R.id.vp2);
viewPager2.setAdapter(new FragmentAdapter(this));
TabLayoutMediator tab = new TabLayoutMediator(tl, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
@Override
public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
switch (position){
case 0:
tab.setText("搜索");
break;
case 1:
tab.setText("新闻");
break;
case 2:
tab.setText("我的");
break;
}
}
});
tab.attach();
}
}
}
需要Fragment加载类
我建了个 Fragment 目录放 fragment 类
新建FragmentAdapter类
package com.example.goodrubish.Fragment;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
public class FragmentAdapter extends FragmentStateAdapter {
public FragmentAdapter(@NonNull FragmentActivity fragmentActivity) {
super(fragmentActivity);
}
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
case 0:
return new SearchFragment();
case 1:
return new NewsFragment();
default:
return new MineFragment();
}
}
@Override
public int getItemCount() {
return 3;
}
}
新建三个 fragment 类,选Fragment(Blank)
搜索布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".Fragment.SearchFragment">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/titleblock"
android:scaleType="fitCenter"
android:layout_gravity="top"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60sp"
android:orientation="horizontal"
android:background="#CCCCCC"
android:layout_marginTop="300dp">
<EditText
android:id="@+id/et_search"
android:layout_width="wrap_content"
android:layout_height="50sp"
android:layout_weight="6"
android:background="#FFFFFF"
android:layout_gravity="center"
android:textSize="25sp"/>
<Button
android:id="@+id/bt_search"
android:layout_width="wrap_content"
android:layout_height="60sp"
android:layout_weight="1"
android:layout_gravity="center"
android:text="分类一下"
android:textSize="15sp"/>
</LinearLayout>
</FrameLayout>
新闻布局
暂时没想好
我的布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".Fragment.MineFragment">
<ImageView
android:layout_width="match_parent"
android:layout_height="100sp"
android:src="@drawable/head"
android:layout_marginTop="80dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="220dp"
android:text="hello"
android:gravity="center"
android:textSize="30sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="历史记录"
android:layout_gravity="center"
android:shadowColor="@color/white"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="退出账号"
android:layout_marginTop="420dp"
android:shadowColor="@color/white"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"/>
</FrameLayout>
越看越丑,算了,就这样,后面有空再去美化美化!!!
Java代码
一、显示用户名
二、实现退出登录功能,历史记录功能等其他功能弄好
前往 minefragment 类,在 onCreateView 方法下多创建一个方法来控制
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_mine, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//在fragment中与在activity中不同,要加上getActicity()
SharedPreferences sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE);
String name = sp.getString("name",null);
TextView textView = getActivity().findViewById(R.id.minetv);
Button button = getActivity().findViewById(R.id.minebttc);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//创建一个弹窗通知
AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).setTitle("退出登录")
.setMessage("是否确定退出账号:"+name)
.setNeutralButton("确定", new DialogInterface.OnClickListener() {//普通按钮会在弹框左边
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent();
intent.setClass(getActivity(), login.class);
startActivity(intent);
getActivity().finish();
}
})
.setPositiveButton("取消", new DialogInterface.OnClickListener() { //yes按钮会在弹框右边
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
}).create();
alertDialog.show();
}
});
textView.setText(name);
}
完成了!!!疫情又严重了,这几天都要全校核酸,一排就是一两小时,还开始上网课了,感觉都没什么心思学习了,拖延症又要开始了,尽可能写吧!