请根据课程实操实现APP门户界面框架设计,至少包含4个tab页,能实现tab页之间的点击切换;
2、技术:使用布局(layouts)和分段(fragment),对控件进行点击监听;
创建视图并通过id联系控件,设置监听,定义四个Fragment并将其压入控件内,对四个linerLayout的点击监听进行重写,以及点击切换fragment和图标。
核心技术fragment控件的使用,多个fragment的创建和压入控件,点击监听的设置
运行结果1.打开界面
2.点击效果
```java
package com.example.myapplication;
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.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Fragment wechatFragment = new WechatFragment();
private Fragment friendFragment = new FriendFragment();
private Fragment contectFragment = new ContectFragment();
private Fragment configFragment = new ConfigFragment();
private FragmentManager fragmentManager;
private LinearLayout linearLayout1;
private LinearLayout linearLayout2;
private LinearLayout linearLayout3;
private LinearLayout linearLayout4;
private ImageView view1;
private ImageView view2;
private ImageView view3;
private ImageView view4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
linearLayout1 = findViewById(R.id.linearLayout1);
linearLayout2 = findViewById(R.id.linearLayout2);
linearLayout3 = findViewById(R.id.linearLayout3);
linearLayout4 = findViewById(R.id.linearLayout4);
view1 = findViewById(R.id.imageView1);
view2 = findViewById(R.id.imageView2);
view3 = findViewById(R.id.imageView3);
view4 = findViewById(R.id.imageView4);
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,wechatFragment);
transaction.add(R.id.content,friendFragment);
transaction.add(R.id.content,contectFragment);
transaction.add(R.id.content,configFragment);
hideFragment(transaction);
transaction.show(wechatFragment);
transaction.commit();
}
protected void hideFragment(FragmentTransaction transaction){
transaction.hide(wechatFragment);
transaction.hide(friendFragment);
transaction.hide(contectFragment);
transaction.hide(configFragment);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.linearLayout1:
showFragment(1);
break;
case R.id.linearLayout2:
showFragment(2);
break;
case R.id.linearLayout3:
showFragment(3);
break;
case R.id.linearLayout4:
showFragment(4);
break;
default:
break;
}
}
protected void showFragment(int i){
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragment(transaction);
switch (i) {
case 1:
setEmpty();
view1.setImageResource(R.drawable.wechat1);
transaction.show(wechatFragment);
break;
case 2:
setEmpty();
view2.setImageResource(R.drawable.friend1);
transaction.show(friendFragment);
break;
case 3:
setEmpty();
view3.setImageResource(R.drawable.config1);
transaction.show(contectFragment);
break;
case 4:
setEmpty();
view4.setImageResource(R.drawable.contect1);
transaction.show(configFragment);
break;
default:
break;
}
transaction.commit();
}
protected void setEmpty(){
view1.setImageResource(R.drawable.wechat);
view2.setImageResource(R.drawable.friend);
view3.setImageResource(R.drawable.config);
view4.setImageResource(R.drawable.contect);
}
}
总结
通过这次作业,我了解了简单的控件布局,并掌握了一些控件的控制方法,setOnClickListener绑定控件以及简单应用。对安卓的调度机制有了一定的认识。
作业源码地址



