Android局部布局替换实现
最近的开发中遇到了这个问题,我采用了一种比较取巧的方法来实现:
在布局文件中设置两个要替换的页面的visibility属性:
<include android:id="@+id/include1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.1" layout="@layout/footer_friendsinfo" /> <include android:id="@+id/include2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.1" android:visibility="gone" layout="@layout/footer_asured_info" />
即一个暂时显示,另一个处于隐藏状态(Android中控件visibility属性的三种状态:
可见(visible)
XML文件:android:visibility="visible"
Java代码:view.setVisibility(View.VISIBLE);
不可见(invisible)
XML文件:android:visibility="invisible"
Java代码:view.setVisibility(View.INVISIBLE);
隐藏(GONE)
XML文件:android:visibility="gone"
Java代码:view.setVisibility(View.GONE);)
接着在相应的Java文件中获取布局对象,设置其Visibility属性:
//更换布局 LinearLayout layout1 = (LinearLayout) v.findViewById(R.id.include1); LinearLayout layout2 = (LinearLayout) v.findViewById(R.id.include2); layout1.setVisibility(View.GONE); layout2.setVisibility(View.VISIBLE);
这样就可以实现局部布局的替换了。
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28