我想根据按钮点击更改片段。 (我制作了两个名为fragmentLeft和fragmentMiddle的片段类)
这是我的MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
}
Fragment fr;
public void selectFrag(View view) {
if(view == findViewById(R.id.bigbutton_left)) {
fr = new fragmentLeft();
}else {
fr = new fragmentMiddle();
}
FragmentManager fm = getFragmentManager();
if (fm != null) {
// Perform the FragmentTransaction to load in the list tab content.
// Using FragmentTransaction#replace will destroy any Fragments
// currently inside R.id.fragment_content and add the new Fragment
// in its place.
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, fr);
ft.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是fragmentLeft.java
public class fragmentLeft extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragmentleft, container, false);
}
}
与fragmenttwo相同,但传递给infater.inflate()的布局ID除外
我已经制作了两个布局文件,也有正确的名称。 这是我的activity_main.xml
中我的片段的定义
但是我无法让按钮做任何事情。没有交易发生,我坚持使用第一个片段。
经过一段谷歌搜索后,我才知道不建议您在布局中使用片段,但是其他一些类型的布局,但是我看到了这个示例代码http://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/ 这似乎工作得很好。
问题是什么?
:: EDIT :: 有一些非常奇怪的事情发生了。 如果我将此布局xml文件用于主要活动:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<fragment
android:name="com.mainpackage.FragmentOne"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
片段会发生变化并按预期运行。 但如果我使用这种布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBaseline="@+id/bigbutton_middle"
android:layout_alignBottom="@+id/bigbutton_middle"
android:layout_marginLeft="7dp"
android:layout_toRightOf="@+id/bigbutton_middle"
android:background="@drawable/mainbutton_right" />
<Button
android:id="@+id/bigbutton_left"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignBaseline="@+id/bigbutton_middle"
android:layout_alignBottom="@+id/bigbutton_middle"
android:layout_marginRight="7dp"
android:layout_toLeftOf="@+id/bigbutton_middle"
android:background="@drawable/mainbutton_left" />
<Button
android:id="@+id/bigbutton_middle"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="187dp"
android:background="@drawable/mainbutton_middle" />
<fragment
android:id="@+id/fragment_place"
android:name="com.mainpackage.FragmentOne"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="@+id/bigbutton_right" />
</RelativeLayout>
片段似乎没有改变,即。按钮似乎已经死了。
可能是什么问题?
我正在使用Eclipse Juno
答案 0 :(得分:2)
您无法获得Buttons
的引用,以便FragmentTransaction
发生。您需要在OnClickListeners
上设置Buttons
,以便他们可以执行相关代码。
第二种布局不显示的一个原因是因为您引用了一个不存在的ID:
<fragment
android:id="@+id/fragment_place"
android:name="com.mainpackage.FragmentOne"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="@+id/bigbutton_right" />
我没有在该xml上看到View
的ID为bigButton_right
。
第一个xml起作用的另一个原因是第二个不起作用,因为你没有在第二个xml上为任何onClick:
设置Button
属性。