Android 子布局可见性在另一个子布局单击事件中消失
我的父级Android 子布局可见性在另一个子布局单击事件中消失,android,android-layout,visibility,onclicklistener,Android,Android Layout,Visibility,Onclicklistener,我的父级Linearlayout1包含两个子帧布局:FrameLayout1和FrameLayout2 FrameLayout1位于FrameLayout2之上,但仅覆盖FrameLayout2的一半 我用一些fragment1替换了FrameLayout1,也用一些fragment2替换了FrameLayout2 现在,当我单击FrameLayout2时,FrameLayout1应该不可见。但这并没有发生 我尝试了以下方法: userDetails.java @Override pu
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侦听器在单击时没有被调用
更新:
活动\u 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>
我认为您必须添加到FrameLayout2 clickable中。尝试添加
android:clickable="true"
然后在代码集中,单击FrameLayout2中的Listener。顺便说一句,我认为您应该将顶部布局和底部布局转换为FrameLayout
,而不是LinearLayout
请注意,您的框架布局应该是最终的,以便侦听器类访问
希望有帮助:)您已将OnClickListener设置为“整体视图”,而不是“底部布局”。我认为你不能将FrameLayout转换为LinearLayout。下面的代码在这里运行良好
final FrameLayout topLayout = (FrameLayout) findViewById(R.id.FrameLayout1);
final FrameLayout bottomLayout = (FrameLayout) findViewById(R.id.FrameLayout2);
bottomLayout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
topLayout.setVisibility(View.GONE);
}
});
你能分享你的xml吗?或者,如果您希望在单击FrameLayout2时FrameLayout1变为不可见,那么您是否应该在bottomLayout中而不是在view中执行setOnClickListener?@user1455909-这两种方法都不起作用。尽管我在OnCreateView上对其进行了初始化,但单击bottomLayout会产生NullPointerException。我还发现只有bottomLayout的顶部标题部分是可单击的。我还发现只有bottomLayout的顶部黑色标题部分是可单击的。这可能是因为在linearlayout中有更多视图需要
android:clickable
参数至false