【问题标题】:Child Layout visibility gone on another child layout click event子布局可见性在另一个子布局单击事件中消失
【发布时间】:2013-06-14 18:39:01
【问题描述】:

我的父级 Linearlayout1 包含两个子框架布局:FrameLayout1FrameLayout2

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


【解决方案1】:

我认为你必须添加到你的 FrameLayout2 可点击。尝试添加

android:clickable="true"

然后在你的 FrameLayout2.setOnClickListener 代码中。顺便说一句,我认为您应该将 topLayout 和 bottomLayout 转换为 FrameLayout 而不是 LinearLayout

请注意,您的框架布局应该是最终的,以便侦听器类可以访问。

希望对你有帮助:)

【讨论】:

  • 我还发现bottomLayout只有顶部的黑色表头部分是可点击的。
  • 这可能是因为您的线性布局中有更多视图需要将android:clickable 参数设置为false
【解决方案2】:

您已将 OnClickListener 设置为整个 View 而不是 bottomLayout。而且我认为您不能将 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);
}
});

【讨论】:

  • 我也发现bottomLayout只有top header部分是可点击的。
猜你喜欢
  • 2013-03-30
  • 2014-12-17
  • 2020-12-24
  • 2015-03-16
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多