一、任务
Kook聊天界面的编辑
二、编写过程
1、创建新的空项目,语言选择java
2、创建Layerout
(1)Bottom
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:layout_width="410dp"
android:layout_height="134dp"
android:layout_marginEnd="2dp"
android:gravity="bottom"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="103dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView7"
android:layout_width="match_parent"
android:layout_height="114dp"
android:src="@tools:sample/avatars"
app:srcCompat="@drawable/img_1" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="聊天" />
</LinearLayout>
<LinearLayout
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="112dp"
android:src="@tools:sample/avatars"
app:srcCompat="@drawable/img_2" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="联系人" />
</LinearLayout>
<LinearLayout
android:layout_width="109dp"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView5"
android:layout_width="match_parent"
android:layout_height="112dp"
android:src="@tools:sample/avatars"
app:srcCompat="@drawable/img_3" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="动态" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView6"
android:layout_width="match_parent"
android:layout_height="112dp"
android:src="@tools:sample/avatars"
app:srcCompat="@drawable/img_4" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
(2)Top
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="134dp"
android:layout_weight="1"
android:gravity="center"
android:text="Kook"
android:textColorLink="#673AB7"
android:textSize="40sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
(3)main_activity
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/top"
android:layout_width="match_parent"
android:layout_height="139dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="457dp">
</FrameLayout>
<include
layout="@layout/bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
(3)Fragment
<?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=".bookFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/hello_blank_fragment"
android:textSize="48sp"
tools:text="这是用户界面" />
</FrameLayout>
3、MainActivity
以下为代码
package com.example.kook;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.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 kookFragment=new kookFragment();
private Fragment bookFragment=new bookFragment();
private Fragment findFragment=new findFragment();
private Fragment myFragment=new myFragment();
private android.app.FragmentManager fragmentManager;
private LinearLayout LinearLayout1,LinearLayout2,LinearLayout3,LinearLayout4;
private ImageView ImageView1,ImageView2,ImageView3,ImageView4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
LinearLayout1=findViewById(R.id.LinearLayout_kook);
LinearLayout2=findViewById(R.id.LinearLayout_book);
LinearLayout3=findViewById(R.id.LinearLayout_find);
LinearLayout4=findViewById(R.id.LinearLayout_my);
ImageView1=findViewById(R.id.imageView7);
ImageView2=findViewById(R.id.imageView4);
ImageView3=findViewById(R.id.imageView5);
ImageView4=findViewById(R.id.imageView6);
LinearLayout1.setOnClickListener(this);
LinearLayout2.setOnClickListener(this);
LinearLayout3.setOnClickListener(this);
LinearLayout4.setOnClickListener(this);
initFragment();
showFragment(0);
}
private void initFragment(){
fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.add(R.id.content,kookFragment);
transaction.add(R.id.content,bookFragment);
transaction.add(R.id.content,findFragment);
transaction.add(R.id.content,myFragment);
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction){
transaction.hide(kookFragment);
transaction.hide(bookFragment);
transaction.hide(findFragment);
transaction.hide(myFragment);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.LinearLayout_kook:
showFragment(0);
break;
case R.id.LinearLayout_book:
showFragment(1);
break;
case R.id.LinearLayout_find:
showFragment(3);
break;
case R.id.LinearLayout_my:
showFragment(4);
break;
default:
break;
}
}
private void showFragment(int i) {
FragmentTransaction transaction=fragmentManager.beginTransaction();
hideFragment(transaction);
switch(i){
case 0:
transaction.show(kookFragment);
ImageView1.setImageResource(R.drawable.img_1);
ImageView2.setImageResource(R.drawable.img_2);
ImageView3.setImageResource(R.drawable.img_3);
ImageView4.setImageResource(R.drawable.img_3);
break;
case 1:
transaction.show(bookFragment);
ImageView2.setImageResource(R.drawable.img_1);
ImageView1.setImageResource(R.drawable.img_2);
ImageView3.setImageResource(R.drawable.img_3);
ImageView4.setImageResource(R.drawable.img_4);
break;
case 3:
transaction.show(findFragment);
ImageView3.setImageResource(R.drawable.img_1);
ImageView1.setImageResource(R.drawable.img_2);
ImageView2.setImageResource(R.drawable.img_3);
ImageView4.setImageResource(R.drawable.img_4);
break;
case 4:
transaction.show(myFragment);
ImageView4.setImageResource(R.drawable.img_1);
ImageView1.setImageResource(R.drawable.img_2);
ImageView2.setImageResource(R.drawable.img_3);
ImageView3.setImageResource(R.drawable.img_4);
break;
default:
break;
}
transaction.commit();
}
}
创建Fragment文件
初始化
private Fragment weixinFragment=new weixinFragment();
private Fragment bookFragment=new bookFragment();
private Fragment findFragment=new findFragment();
private Fragment myFragment=new myFragment();
private android.app.FragmentManager fragmentManager;
private LinearLayout LinearLayout1,LinearLayout2,LinearLayout3,LinearLayout4;
private ImageView ImageView1,ImageView2,ImageView3,ImageView4;
各个模块之间的属性读取
三、运行结果
三、总结
1.以老师课堂上讲的内容为依据,在自己的电脑上完成相应操作,比自己设计要轻松一点。
2.尽管如此,还是会出现一些错误,比如图片不显示和bottom显示不出的问题,在正文中已经写下了解决方案。
3.粗心会漏掉代码,比如transaction.commit();