【问题标题】:click on empty space in fragment executes activity event单击片段中的空白区域执行活动事件
【发布时间】:2016-11-10 06:48:21
【问题描述】:

我有一个Activity,它由 4 张图片和onClick 组成,我正在打开一个Fragment。另外,我有一个Navigation Drawer,其中配置文件选项打开了一个fragment。由于我的ProfileFragment 中的字段很少,因此分配了空白空间。如果用户单击空白区域,则执行放置在Activity 中的图像的单击事件,并打开特定的Fragment。我尝试将profilefragment 的背景颜色设置为white,但没有成功。

ProfileFragment 类:

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_profile, container, false);
    ButterKnife.bind(this, view);
    view.setBackgroundColor(Color.WHITE);
    return view;
}

profile_fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.six30labs.mis.fragments.ProfileFragment">  

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

               <EditText
                        android:id="@+id/empCompanyName"
                        style="@style/MyTextViewStyle"                            
                        android:hint="Company Name"
                        android:inputType="text" />


               <EditText
                        android:id="@+id/empPhoneNumber"
                        style="@style/MyTextViewStyle"
                        android:hint="Employee PhNo"
                        android:inputType="text" />

               <EditText
                        android:id="@+id/empEmail"
                        style="@style/MyTextViewStyle"
                        android:hint="Employee Email"
                        android:inputType="text" />

             <Button
                     android:id="@+id/editProfileBtn"
                     android:text="Edit"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                      />
   </LinearLayout>
 </FrameLayout>

单击Navigation Drawer 中的配置文件选项时,我使用以下行打开片段:

   fragment = new ProfileFragment();
 getSupportFragmentManager().beginTransaction().replace(R.id.container_view,fragment).addToBackStack("Profile").commit();

我该如何解决这个问题?请帮帮我..

【问题讨论】:

标签: android android-fragments android-activity


【解决方案1】:

在片段的根布局中编写此代码

android:clickable="true"

在你的情况下是FrameLayout 所以

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    tools:context="com.six30labs.mis.fragments.ProfileFragment">  

【讨论】:

  • 嘿,谢谢!解决了这个问题!一定会读到的:)
猜你喜欢
  • 2015-12-04
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 2015-11-30
  • 2016-12-30
  • 2020-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多