Android 局部布局替换的实现方式

  最近再搞远程视频的功能,其中要实现加载视频、加载视频失败、加载成功的局部布局替换,查阅相关资料,找到一种投机取巧的方式。

  首先分别写这三种效果的子布局,分别为

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);
这样就实现了想要实现的效果哟!!!
 

转载于:https://www.cnblogs.com/dmrbell/p/11286699.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 中,可以使用布局替换来动态更改视图的布局。以下是一些步骤: 1. 首先,定义一个父视图,该视图将包含两个或多个布局。例如,可以使用LinearLayout或RelativeLayout作为父视图。 2. 定义要替换布局文件。例如,可以创建一个新的布局文件或重复使用现有的布局文件。 3. 在父视图中添加一个占位符视图,用于显示要替换布局文件。可以使用FrameLayout、LinearLayout或RelativeLayout等视图来充当占位符。 4. 在代码中,使用FragmentTransaction类启动一个新的Fragment,并将其添加到占位符视图中。 5. 在Fragment中,使用onCreateView()方法加载要替换布局文件。 6. 当需要更改布局时,使用FragmentTransaction类启动另一个Fragment,并将其替换为当前Fragment。可以使用replace()方法进行布局替换。 下面是一个简单的示例代码: 1. 定义父视图: ``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/placeholder" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> ``` 2. 定义要替换布局文件: ``` <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="This is the first layout." /> </LinearLayout> ``` 3. 在代码中启动Fragment: ``` FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragment firstFragment = new MyFragment(); fragmentTransaction.add(R.id.placeholder, firstFragment); fragmentTransaction.commit(); ``` 4. 在Fragment中加载布局文件: ``` @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.my_layout, container, false); } ``` 5. 更改布局: ``` FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); MyFragment secondFragment = new MyFragment(); fragmentTransaction.replace(R.id.placeholder, secondFragment); fragmentTransaction.commit(); ``` 注意,以上示例代码仅供参考,实际实现可能会因您的具体需求而有所不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值