最近在搞远程视频的功能,其中要实现加载视频、加载视频失败、加载成功的局部布局替换,查阅相关资料,找到一种投机取巧的方式。
首先分别写这三种效果的子布局,分别为
top_remotetreate.xml,
top_attemptconnect.xml,
top_failcon.xml
然后在主布局中include这三个xml,如下:
<include
android:id="@+id/inc_remotetreate"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
layout="@layout/top_remotetreate" />
<include
android:id="@+id/inc_attemptconnect"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:visibility="gone"
layout="@layout/top_attemptconnect" />
<include
android:id="@+id/inc_failconnect"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:visibility="gone"
layout="@layout/top_failcon" />
即一个暂时显示,另两个处于隐藏状态(Android中控件的visibility属性的三种状态:可见(visible),不可见(invisible),隐藏(gone)
最后在相应的Java文件中获取布局对象,设置其Visibility属性:
private LinearLayout inc_remotetreate,inc_attemptconnect,inc_failconnect;
inc_remotetreate = view.findViewById(R.id.inc_remotetreate);
inc_attemptconnect = view.findViewById(R.id.inc_attemptconnect);
inc_failconnect = view.findViewById(R.id.inc_failconnect);
inc_remotetreate.setVisibility(View.GONE);
inc_attemptconnect.setVisibility(View.VISIBLE);
inc_failconnect.setVisibility(View.GONE);
这样就实现了想要实现的效果哟!!!