【发布时间】:2016-01-27 19:30:01
【问题描述】:
我是一个初学者,我正在从tab_fragment.xml 为按钮充气,当我尝试为该按钮设置 OnClickListener 时,它不起作用
LayoutInflater inflater=getLayoutInflater();
View v=inflater.inflate(R.layout.tab_fragment, (ViewGroup) findViewById(R.id.line1));
Button button= (Button) v.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFrag(new Tab(), "two");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
});
这是我的 tab_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/line1"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_margin="50dp"
android:text="some text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
【问题讨论】:
标签: android button onclicklistener android-inflate