Android重叠布局
我有一个大约有5页的滑动寻呼机。每个布局的充气方式如下所示:Android重叠布局,android,layout,inflate,Android,Layout,Inflate,我有一个大约有5页的滑动寻呼机。每个布局的充气方式如下所示: public static class SectionFragment extends Fragment { ... @Override public View onCreateView(LayoutInflater inflater, ...) { ... rootView = inflater.inflate(R.layout.gridpage1,container,false);
public static class SectionFragment extends Fragment {
...
@Override
public View onCreateView(LayoutInflater inflater, ...) {
...
rootView = inflater.inflate(R.layout.gridpage1,container,false);
...
}
现在我想检查一个条件是否为真,如果为真,我想先膨胀gridpage1布局,然后再膨胀上面的另一个布局
我该怎么做?我所需要的只是帮助将两个视图依次展开。您可以在主布局中使用
标记,然后使用设置可见性(View.GONE/VISIBLE)
隐藏/显示所需的视图。
例如:
<RelativeLayout 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="@string/hello_world" />
<include android:id="@+id/gridpage1_layout" layout="@layout/gridpage1"/>
<include android:id="@+id/gridpage2_layout" layout="@layout/gridpage2"/>
...
</RelativeLayout>
...
在您的片段中,您只能膨胀根布局并按ID查找其他视图。膨胀视图基本上只意味着从XML文件创建视图并返回它 在您的特定情况下,您只需要从
onCreateView
函数返回片段内容视图。这必须是一个视图,因此如果您的条件为真,并且需要两个视图,请执行以下操作:
FrameLayout
以编程方式查看自己
类似于:FrameLayout=newframelayout(上下文)代码>
框架布局
执行frameLayout.addView(充气器.充气(R.layout.gridpage1,frameLayout,false))代码>
甚至充气机充气(R.layout.gridpage1,frameLayout,true)
足够了,因为true
告诉它将视图添加到容器中
框架布局
onCreate视图返回FrameLayout
View v1 = inflater.inflate(R.layout.gridpage1,frameLayout,false);
this.v1Reference = v1;
frameLayout.addView(v1);
备选案文2:
inflater.inflate(R.layout.gridpage1,frameLayout,true);
this.v1Reference = frameLayout.findViewById(...);
使用此选项,可以隐藏视图吗?我只想在任何给定的时间显示1。喜欢切换开关…当然,您可以保留对每一个的引用,或者使用其id找到它,然后调用view.setVisibility(..)好的,我会提前给出,如果需要帮助,我会发布。谢谢,另一个选项不是隐藏和显示frameLayout中的每个视图,而是相应地调用removeView(v1)和addView(v2)。。只要您保留对v1和v2的引用,它们就不会是免费的。有一个问题,在我添加到frameLayout
ViewStub
时,如何获取对视图的引用可能更适合这种情况。