【发布时间】:2013-06-14 18:39:01
【问题描述】:
我的父级 Linearlayout1 包含两个子框架布局:FrameLayout1 和 FrameLayout2。
FrameLayout1 位于FrameLayout2 之上,但只覆盖了FrameLayout2 的一半。
我用一些fragment1 替换了FrameLayout1,还用一些fragment2 替换了FrameLayout2。
现在,当我点击 FrameLayout2 时,FrameLayout1 应该会变得不可见。但这并没有发生。
我尝试了以下方法:
userDetails.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.userDetails, container, false);
topLayout = (LinearLayout) getActivity().findViewById(R.id.FrameLayout1);
bottomLayout = (LinearLayout) getActivity().findViewById(R.id.FrameLayout2);
view.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
topLayout.setVisibility(View.GONE);
}
});
}
我还发现 onClick 的 view 监听器在点击时没有被调用。
更新:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/FrameLayout1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/FrameLayout2"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</FrameLayout>
【问题讨论】:
-
你能分享你的xml吗?或至少一部分
-
如果您希望 FrameLayout1 在单击 FrameLayout2 时不可见,您不应该在 bottomLayout 中执行 setOnClickListener 而不是 view 吗?
-
@user1455909 - 两种方式都行不通。尽管我在 OnCreateView 上对其进行了初始化,但单击 bottomLayout 却给出了 NullPointerException。
标签: android android-layout visibility onclicklistener