重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
郑州app开发android使用fragment模拟微信界面。
成都创新互联-专业网站定制、快速模板网站建设、高性价比青海网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式青海网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖青海地区。费用合理售后完善,10多年实体公司更值得信赖。
思路:布局一共是5个页面,其中四个页面几乎相同,只有activity_main.xml不一样需要特殊处理。下面是activity_main.xml。
{?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:id="@+id/ll_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignparentbottom="true"
android:background="@drawable/menu_bg"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintbottom_tobottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"}
{Imagebutton
android:id="@+id/ib_weixin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/weixin_normal"
/}
{Imagebutton
android:id="@+id/ib_contact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/contact_list_normal"
/}
{Imagebutton
android:id="@+id/ib_discover"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/find_normal"
/}
{Imagebutton
android:id="@+id/ib_me"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/profile_normal"
/}
{/LinearLayout}
{RelativeLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/ll_button"
}
{/RelativeLayout}
{/androidx.constraintlayout.widget.ConstraintLayout}
因为剩余四个页面几乎一样,在这里就展示其中一个fragment_contact.xml
{?xml version="1.0" encoding="utf-8"?}
{RelativeLayout 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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"}
{TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="联系人"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /}
{/RelativeLayout}
下面是java代码,因为四个页面都一样,只展示一个FindFragment.java
package cn.xhhkj.videoview;
import android.os.bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class FindFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_find, null);
return view;
}
}
MainActivity.java
package cn.xhhkj.videoview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.app.FragmentTransaction;
import android.media.Mediaplayer;
import android.os.bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Imagebutton;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "结果是";
private Imagebutton ib_weixin;
private Imagebutton ib_contact;
private Imagebutton ib_find;
private Imagebutton ib_me;
private ContactFragment contactFragment;
private WeixinFragment weixinFragment;
private FindFragment findFragment;
private MeFragment meFragment;
@Override
protected void onCreate(bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
ib_weixin = findViewbyId(R.id.ib_weixin);
ib_contact = findViewbyId(R.id.ib_contact);
ib_find = findViewbyId(R.id.ib_discover);
ib_me = findViewbyId(R.id.ib_me);
ib_contact.setOnClickListener(this);
ib_find.setOnClickListener(this);
ib_me.setOnClickListener(this);
ib_weixin.setOnClickListener(this);
//用键盘模拟人按下的场景
ib_weixin.performClick();
}
@Override
public void onClick(View v){
clearIcon();
FragmentManager manager = getSupportFragmentManager();
androidx.fragment.app.FragmentTransaction transaction = manager.beginTransaction();
switch (v.getId()){
case R.id.ib_contact:
if(contactFragment==null){
contactFragment = new ContactFragment();
}
transaction.replace(R.id.fragment_container, contactFragment);
ib_contact.setImageResource(R.drawable.contact_list_pressed);
break;
case R.id.ib_weixin:
if(weixinFragment==null){
weixinFragment = new WeixinFragment();
}
transaction.replace(R.id.fragment_container, weixinFragment);
ib_weixin.setImageResource(R.drawable.weixin_pressed);
break;
case R.id.ib_discover:
if(findFragment == null){
findFragment = new FindFragment();
}
transaction.replace(R.id.fragment_container, findFragment);
ib_find.setImageResource(R.drawable.find_pressed);
break;
case R.id.ib_me:
if(meFragment==null){
meFragment = new MeFragment();
}
transaction.replace(R.id.fragment_container, meFragment);
ib_me.setImageResource(R.drawable.profile_pressed);
break;
}
transaction.commit();
}
private void clearIcon(){
ib_contact.setImageResource(R.drawable.contact_list_normal);
ib_find.setImageResource(R.drawable.find_normal);
ib_me.setImageResource(R.drawable.profile_normal);
ib_weixin.setImageResource(R.drawable.weixin_normal);
}
}
总结,用键盘模拟人按下的场景ib_weixin.performClick();这个performClick()方法,是用键盘模拟人按下的效果。