【发布时间】:2014-11-18 10:23:15
【问题描述】:
我是片段新手,我尝试使用这些片段。 我来自活动的 xml 是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Loguin"
tools:ignore="MergeRootFrame">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Test"
android:id="@+id/btn_FragmentTest"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_marginBottom="0dp" />
<LinearLayout
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"
android:orientation="horizontal"
android:id="@+id/frgContainer"
android:layout_marginTop="20dp">
<fragment
android:id="@+id/frgLoguin"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="habitatprojects.hubbuildings.Loguin$PlaceholderFragment"
tools:layout="@layout/fragment_loguin" />
</LinearLayout>
</LinearLayout>
我想改变LinearLayout里面的片段,我的mainActivity类是:
public class Loguin extends Activity {
private int num = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loguin);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
Button btnBoton1 = (Button)findViewById(R.id.btn_FragmentTest);
btnBoton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0)
{
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(num==0)
{
fragmentTransaction.replace(R.id.frgContainer, new PlaceholderFragment())
.commit();
num++;
}
else{
if(num==1)
{
fragmentTransaction.replace(R.id.frgContainer, new PlaceholderFragment2())
.commit();
num++;
}
else{
fragmentTransaction.replace(R.id.frgContainer, new PlaceholderFragment3())
.commit();
num = 0;
}
}
}
});
}
我的 PlaceHolderFunction 是:
public static class PlaceholderFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_loguin, container, false);
}
}
public static class PlaceholderFragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
public static class PlaceholderFragment3 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank2, container, false);
}
}
为什么我按下按钮不起作用?我做错什么了?对不起我的英语,提前谢谢!
PD:如果您需要更多代码或信息,请告诉我,谢谢!
PD1:当我使用我的设备进行调试时,日志猫只会说我:
11-18 11:33:30.775 6065-6065/habitatprojects.hubbuildings I/ViewRootImpl:ViewRoot 的触摸事件:Touch Down 11-18 11:33:30.835 6065-6065/habitatprojects.hubbuildings I/ViewRootImpl:ViewRoot's 触摸事件:Touch UP
【问题讨论】:
-
预期行为是什么,你得到了什么?如果遇到异常,请添加堆栈跟踪表单 logcat。
-
感谢模拟您现在有我的 LogCat !没有错误...:D
标签: java android android-fragments android-linearlayout android-button