一、项目介绍
此项目是安卓APP的门户界面框架设计,包含4个tabl,能简单实现table页面之间的点击切换功并使用布和分,对控件进行点击监听。页面效果如下:
二、页面展示
1、顶栏设计
新建文件后使用在布局小组件俩面拖一个LinearLayout,然后在LinearLayout之下拖一个textView进来,具体代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
android:background="@color/teal_200"
android:gravity="center"
android:text="@string/textView"
android:textAlignment="center"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
效果如图:
2、底栏设计
准备四个图标,新建文件后在底部加入组件,每个区块中将图片加入进去,最后设置字体与布局,主要代码如下:
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@android:color/darker_gray">
<LinearLayout
android:id="@+id/first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/messige" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/textView3"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/friend" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/textView4"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/third"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/finding" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/textView5"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/fourth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/my" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/textView6"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
效果如下:
3、主题设计
在布局小组件里面拖一个LinearLayout,在此布局之下拖一个FragmentLayout并将其id改为content并引入之前的顶部界面以及底部界面
代码如下:
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<include layout="@layout/top"></include>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<include layout="@layout/bottom"></include>
</LinearLayout>
图片效果如下:
4、四个fragment页面
点击按钮后我们需要跳转到对应的界面中去,所以需要单独设计界面
(1)聊天
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".first">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="聊天"
android:textSize="50sp" />
</LinearLayout>
图片效果如下:
(2)通讯录
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".first">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="通讯录"
android:textSize="50sp" />
</LinearLayout>
图片效果如下:
(3)朋友圈
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".first">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="朋友圈"
android:textSize="50sp" />
</LinearLayout>
图片效果如下:
(4)我的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".first">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="我的"
android:textSize="50sp" />
</LinearLayout>
图片效果如下:
三、关键部分代码
跳转页需要将布局按钮与事件相结合,当点击按钮时出发监听事件,实现跳转,此时就需要为每个linearLayout与java类绑定,在java文件中写方法实现跳转。
微信部分:
package com.example.myfirstapplication;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class first extends Fragment {
public first() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
}
通讯录
package com.example.myfirstapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class second extends Fragment {
public second() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false);
}
}
发现
package com.example.myfirstapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class third extends Fragment {
public third() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_third, container, false);
}
}
我的
package com.example.myfirstapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class fourth extends Fragment {
public fourth() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fourth, container, false);
}
}
主体代码:
package com.example.myfirstapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Fragment first = new first();
private Fragment second = new second();
private Fragment third = new third();
private Fragment fourth = new fourth();
private FragmentManager fragmentManager;
private LinearLayout linearLayout1;
private LinearLayout linearLayout2;
private LinearLayout linearLayout3;
private LinearLayout linearLayout4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout1 = findViewById(R.id.first);
linearLayout2 = findViewById(R.id.second);
linearLayout3 = findViewById(R.id.third);
linearLayout4 = findViewById(R.id.fourth);
linearLayout1.setOnClickListener(this);
linearLayout2.setOnClickListener(this);
linearLayout3.setOnClickListener(this);
linearLayout4.setOnClickListener(this);
initFragment();
}
protected void initFragment() {
fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.content,first);
transaction.add(R.id.content,second);
transaction.add(R.id.content,third);
transaction.add(R.id.content,fourth);
hideFragment(transaction);
transaction.show(second);
transaction.commit();
}
protected void hideFragment(FragmentTransaction transaction){
transaction.hide(first);
transaction.hide(second);
transaction.hide(third);
transaction.hide(fourth);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.first:
showFragment(1);
case R.id.second:
showFragment(2);
case R.id.third:
showFragment(3);
case R.id.fourth:
showFragment(4);
}
}
private void showFragment(int i) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragment(transaction);
switch (i){
case 1:
transaction.show(first);
break;
case 2:
transaction.show(second);
break;
case 3:
transaction.show(third);
break;
case 4:
transaction.show(fourth);
break;
}
transaction.commit();
}
}
四、项目总结
总体来说这次的实验是一个比较简单的项目,但是其中也有不少需要注意的问题,例如各页面间的切换与显示的问题,但是经过跟同学的交流和自己查询,还是解决了这些问题。