我使用以下代码创建浮动操作按钮。它在相对布局内。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/tools"
android:background="@drawable/form_bg">
<android.support.design.widget.FloatingActionButton
android:id="@+id/myFAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/fab_bg_mini"
app:elevation="4dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/trans_white"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<WebView
android:id="@+id/chart"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
以下是我如何在onCreateView方法中为浮动操作按钮设置onClickListener。
public class TrendChart extends Fragment {
private View mRootview;
private static final String URL = "file:///android_asset/taskstatus.html";
private WebView chart;
class InnerWebView extends WebViewClient {
InnerWebView() {
}
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript: loadTrendChart();");
}
}
public static final TrendChart newInstance(String company_name) {
TrendChart f = new TrendChart();
f.setArguments(new Bundle(1));
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setHasOptionsMenu(true);
mRootview = inflater.inflate(R.layout.trend_chart, null);
this.chart = (WebView) mRootview.findViewById(R.id.chart);
this.chart.getSettings().setJavaScriptEnabled(true);
this.chart.setWebChromeClient(new WebChromeClient());
this.chart.setWebViewClient(new InnerWebView());
refreshWebView();
return mRootview;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FloatingActionButton myFab = (FloatingActionButton) getActivity().findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("Clicked","Hello");
}
});
}
private void refreshWebView() {
this.chart.loadUrl(URL);
}
}
问题是onClickListener没有被触发。为什么会这样?我尝试将监听器放在onCreateView方法中,但问题仍然存在。有没有什么办法可以在扩展Fragments的类中使用浮动按钮?
答案 0 :(得分:2)
完全创建Activity
后,您必须按如下方式放置代码:
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
FloatingActionButton myFab = (FloatingActionButton) getActivity().findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("Check","Clicked");
}
});
}
如果您的代码位于onCreateView()
,则代码应如下所示:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.YOUR_LAYOUT, container, false);
FloatingActionButton myFab = (FloatingActionButton) view.findViewById(R.id.myFAB);
myFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do stuff
}
});
return view;
}
改变这个:
mRootview = inflater.inflate(R.layout.trend_chart, null);
到此:
mRootview = inflater.inflate(R.layout.trend_chart, container, false);