【发布时间】:2017-01-08 17:28:39
【问题描述】:
我应该在哪里为包含的布局创建onClickListeners?我在片段内部尝试过,但我无法通过findViewById。所以我尝试了 Main Activity 但我不确定如何从那里进入包含的布局。
我也在片段中试过这个:
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>
【问题讨论】:
-
如果您想在片段 xml 中添加该视图,您应该将其包含在片段类中
-
@Charuka 好的,但它应该是什么样子,因为我的例子不正确
-
没有得到您的要求?你的意思是在代码中?
-
你红了我的问题吗?您可以看到我尝试创建 onclicklisteners 但它不起作用,那我该怎么办呢?还有为什么是-1分?
-
我把它弄红了,我问的是你最后的评论,不是你的问题 -1 不是来自我:) “它应该是什么样子,因为我的例子不正确”
标签: android android-layout android-fragments onclicklistener