fragment实现tab功能
分类: Android 标签: fragment实现tab功能
2022-08-24 12:10:48 905浏览
fragment实现tab功能,可理解为iframe
fragment实现tab功能,可理解为iframe
fragment代码,menu1.java - men4.java,指向不同的layout xml
public class Menu1 extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.menu1,null);
return v;
}
}
menu1.xml(-menu4.xml就是图片不一样)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:src="@drawable/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
mian.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/mainc"
android:name="s.jf3q.fragment_tab.Menu1"//默认显示的页面
android:layout_width="match_parent"
android:layout_height="match_parent"></fragment>
<LinearLayout
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_weight="1"
android:id="@+id/bm1"
android:text="m1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:id="@+id/bm2"
android:text="m2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:id="@+id/bm3"
android:text="m3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:layout_weight="1"
android:id="@+id/bm4"
android:text="m4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
main.java
package s.jf3q.fragment_tab;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
public class MainActivity extends AppCompatActivity {
FragmentTransaction t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bm1=findViewById(R.id.bm1);
bm1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Menu1 menu1=new Menu1();
t= getSupportFragmentManager().beginTransaction();
t.replace(R.id.mainc,menu1);
t.commit();
}
});
Button bm2=findViewById(R.id.bm2);
bm2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Menu2 menu2=new Menu2();
t= getSupportFragmentManager().beginTransaction();
t.replace(R.id.mainc,menu2);
t.commit();
}
});
Button bm3=findViewById(R.id.bm3);
bm3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Menu3 menu3=new Menu3();
t= getSupportFragmentManager().beginTransaction();
t.add(R.id.mainc,menu3);
t.commit();
}
});
Button bm4=findViewById(R.id.bm4);
bm4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Menu4 menu4=new Menu4();
t= getSupportFragmentManager().beginTransaction();
t.replace(R.id.mainc,menu4);
t.commit();
}
});
}
}

好博客就要一起分享哦!分享海报
此处可发布评论
评论(0)展开评论
暂无评论,快来写一下吧
展开评论
您可能感兴趣的博客
他的专栏
他感兴趣的技术

java
vue
springboot
Mysql
ssm
小程序
uniapp
js和jquery