mit*_*tim 15 android android-layout android-fragments
我有一个问题,我可以创建一个片段,它的视图似乎被创建,但它没有显示.片段本身是创建的,内部的任何代码都没有问题,但它只是在某处看不见.后退按钮也可以很好地与它交互(它"关闭"它),它只是没有物理地显示在屏幕上(只显示主要布局).
从我的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();
}
Run Code Online (Sandbox Code Playgroud)
这是首次显示的View的布局(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>
Run Code Online (Sandbox Code Playgroud)
这是我的片段类:
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)
}
Run Code Online (Sandbox Code Playgroud)
这是"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>
Run Code Online (Sandbox Code Playgroud)
我是新手使用片段,不太确定我的问题是在代码中还是与布局有关.我的目标是将"AddNamePane"添加到"根布局",暂时替换主视图.我尝试这样做是为了给我的根布局一个id并在FragmentTransaction中使用它.如果重要的话,我使用的api级别是8(Android 2.2),因此使用android.support.v4.app包中的东西.
Pau*_*rke 19
你试图以编程方式替换a LinearLayout,同时也期望它有硬编码的孩子.这不行.你应该使用一个FrameLayout也是你FragmentTransaction的孩子layout_root.
如果您使用实际的xml更新帖子,我可以向您展示如何执行此操作.
我有一个类似的问题,当片段没有出现时。问题是我删除了 AndroidAnnotations,但忘记将代码从 onViewCreated 和 onCreate 方法移动到 onCreateView。我通过比较两个片段的代码发现了这个错误 - 一个出现了,另一个没有。所以,如果你有类似的问题,试着检查片段的代码。乍一看,它可能不会显示任何错误,但由于这些错误,它仍然无法正常工作或出现。
特别是,检查你是否有这个方法并且你膨胀了一个视图:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.your_fragment_layout, container, false);
return v;
}
Run Code Online (Sandbox Code Playgroud)