我应该在哪里为包含的布局创建onClickListeners
?我尝试了片段,但是我无法通过findViewById
完成。所以我尝试从主活动,但我不知道如何从那里获得包含布局。
我也在片段中尝试了这个:
public class MainMenu extends Fragment implements View.OnClickListener{
View button_call;
@Override
public View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedinstanceState) {
View myView = inflater.inflate(R.layout.fragment_main_menu, container, false);
button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(this);
return myView;
}
@Override
public void onClick(View v) {
// implements your things
}
public MainMenu() {
}
}
但是Fragment似乎是空的
片段XML:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="@+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingRight="60dp">
<include android:id="@+id/btn_call"
layout="@layout/call_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"/>
<include android:id="@+id/button2"
layout="@layout/message_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
<include android:id="@+id/button3"
layout="@layout/navigate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<include android:id="@+id/button4"
layout="@layout/remind_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30dp"
android:paddingBottom="30dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
</LinearLayout>
</ScrollView>
答案 0 :(得分:2)
<强>步骤强>
layout="@layout/
示例强>
在我的片段中,我在其中有一个include
标记的XML。它连接到我的web.xml,而..web.xml的父标记/ root标记是FrameLayout
..
现在看看我如何初始化我的include
标记..(如果你不使用右根标记来初始化include,它会因空指针而崩溃)
我的web.xml中有一个名为g_betta
的ID(我在片段中包含的XML布局)
看看我如何访问该视图..
public class FragmentAbout extends Fragment {
private RelativeLayout relativeLayoutConnectedInsideIncludeTag;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_fragment,container,false);
FrameLayout view =(FrameLayout) rootView.findViewById(R.id.include_tag);
relativeLayoutConnectedInsideIncludeTag = (RelativeLayout) view.findViewById(R.id.g_betta);
relativeLayoutConnectedInsideIncludeTag.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Yes I Found you", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
my fragment xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/mainmenu_layout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<include
android:id="@+id/include_tag"
layout="@layout/web"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
我的包含 - &gt;的web.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/maroon"
android:gravity="center_horizontal">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/g_betta"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="HELLO"
android:textColor="#FFF"
android:textSize="20dp" />
<RelativeLayout
android:id="@+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"
android:background="@color/colorPrimaryDark"
android:gravity="bottom"
android:paddingLeft="24dp"
android:paddingRight="24dp" />
</RelativeLayout>
<ImageView
android:id="@+id/imageone"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@+id/lin"
android:layout_margin="16dp"
android:background="@drawable/girl"
android:scaleType="fitXY" />
<ImageView
android:id="@+id/imagetwo"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@+id/imageone"
android:layout_margin="16dp"
android:background="@drawable/amanda"
android:scaleType="fitXY" />
</RelativeLayout>
</ScrollView>
</FrameLayout>
答案 1 :(得分:2)
这样做:
button_call = myView.findViewById(R.id.btn_call);
button_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// write your code here...
});