我在创建片段时遇到问题,它的视图似乎已创建,但没有显示。片段本身已创建,并且内部的任何代码都可以正常运行,但它只是在某处不可见。后退按钮也可以很好地与它交互(它“关闭”它),它只是没有物理显示在屏幕上(仅显示主布局)。
从我的 FragmentActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_view);
// some logic here that sets a button listener that calls openAddNamePane() when pressed
}
public void openAddNamePane(){
AddNamePane addNamePane = new AddNamePane();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.layout_root, addNamePane, "AddnamePane");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
这是第一次显示的视图的布局(activity_main_view.xml 又名我的“根布局”):
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="@color/white"
android:id="@+id/layout_root">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/addPlayerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Player" />
<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/buttonLabel" />
</LinearLayout>
<ListView android:id="@+id/playerListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@color/white"
android:entries="@array/testStringArray" >
</ListView>
</LinearLayout>
这是我的片段类:
public class AddNamePane extends Fragment {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("AddNamePane.onCreateView()");
View view = inflater.inflate(R.layout.add_player_pane_layout, container, false);
return view;
}
// a few other methods here, onAttach(), onStop(), onPause(), etc
// all are empty of logic with just a println to see if they are being called correctly (in which they are)
}
这是“AddNamePane”片段的布局:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:text="test name here"
android:hint="Name here" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
我对使用片段很陌生,不太确定我的问题是在代码中还是与布局有关。我的目标是将“AddNamePane”添加到“根布局”中,临时替换主视图。我尝试这样做是给我的根布局一个 id 并在 FragmentTransaction 中使用它。如果重要的话,我使用的 api 级别是 8(Android 2.2),因此使用了 android.support.v4.app 包中的东西。