Andoid中利用Frament实现tab标签切换的效果

一、建立每个tab页对应的Fragment
首先Fragment的存在依赖于activity
创建一个fragment需要继承 Fragment,并复写  onCreateView方法,该方法返回一个view作为返回一个view显示到Activity中,示例

package com.android.fragmentnormal;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment{
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
           return inflater.inflate(R.layout. frament1, container,false);
     }
}




同样的方法可以建立 Fragment2, Fragment3

R.layout.   frament1代表fragment的布局,和普通的activity布局方式一样


这样就建立了三个fragment

下面介绍如何将这三个放入到activity中并且能够互相切换

首先在主 activity_main的布局文件中相应的位置中添加FrameLayout控件


 <FrameLayout
        android:id= "@+id/fragment_container"
        android:layout_width= "fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        ></FrameLayout>



FrameLayout的位置和大小代表了Fragment的位置和大小 

然后再在底部添加切换按钮的布局,效果如图

在activity中显示frgment
如果要兼容3.0版本以前的系统,主activity需要继承 FragmentActivity


在将fragment添加到activity之前,需要先获得 FragmentTransaction对象,


FragmentManager manager = getSupportFragmentManager() ;
FragmentTransaction transaction = manager .beginTransaction() ;
Fragment1 f1 = new Fragment1();  
transaction.add(R.id. fragment_container, f1 );
transaction.addToBackStack( null);
transaction.commit();


这样就将f1加入到了fragment中,如果实现单击按钮切换界面的效果需要给button绑定监听事件,在事件
中调用replace方法实现界面切换,注意:在每次切换前,要重新开始一个事物,一个事物commit之后就不能再次使用,必须重新调用 beginTransaction()方法,并且需要commit来执行事物

transaction = manager .beginTransaction();
transaction.replace(R.id. fragment_container ,f2 );
transaction.commit();


最终效果








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用OpenCV库的VideoCapture类来显示rtsp流。下面是一个示例代码: ```java import org.opencv.core.Mat; import org.opencv.videoio.VideoCapture; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class RtspStreamViewer { public static void main(String[] args) { // Load the OpenCV library System.loadLibrary("opencv_java452"); // Create a VideoCapture object to read from the rtsp stream VideoCapture cap = new VideoCapture("rtsp://username:password@ip_address:port/stream"); // Check if the VideoCapture object was successfully created if(!cap.isOpened()) { System.out.println("Failed to open rtsp stream!"); return; } // Create a window to display the frames String windowName = "RTSP Stream"; Imgproc.namedWindow(windowName); // Read and display the frames from the rtsp stream Mat frame = new Mat(); while(cap.read(frame)) { Imgproc.cvtColor(frame, frame, Imgproc.COLOR_BGR2GRAY); Imgproc.putText(frame, "Press Q to Exit", new org.opencv.core.Point(10, 50), org.opencv.imgproc.Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new org.opencv.core.Scalar(255, 255, 255), 2); Imgcodecs.imshow(windowName, frame); if(Imgcodecs.waitKey(25) == 'q') { break; } } // Release the resources cap.release(); Imgcodecs.destroyAllWindows(); } } ``` 在这个示例代码,我们首先加载了OpenCV库。然后,我们创建了一个VideoCapture对象来读取rtsp流。如果VideoCapture对象创建失败,则打印错误消息并退出程序。接下来,我们创建了一个名为“RTSP Stream”的窗口来显示帧。然后,我们读取并显示rtsp流的帧。我们首先将帧转换为灰度图像,然后将一条文本消息添加到图像。最后,我们使用imshow()方法将帧显示在窗口。如果用户按下“q”键,则退出程序。 请注意,你需要将示例代码的“username”,“password”,“ip_address”和“port”替换为你的rtsp流的凭据和地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值