具有多个片段的Actionbar:片段布局向上移动

时间:2012-12-16 08:32:48

标签: android android-fragments

我有一个运行以下代码的Actionbar:

public class MainActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

    ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayShowTitleEnabled(false);

    bar.addTab(bar.newTab()
        .setText("Fragment_1")
        .setTabListener(
                new TabListener<ExampleFragment>(this, "Fragment_1", ExampleFragment.class)));
  .
  .
  .
   <Listener code that add the Fragment via FragmentTransaction>..
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.options_menu, menu);

    MenuItem mSearchItem = menu.findItem(R.id.menu_fragments);
    edit = (EditText) mSearchItem.getActionView().findViewById(R.id.edit);
    return super.onCreateOptionsMenu(menu);
 }

}

我的菜单options_menu.xml是这样的:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

   <item
       android:id="@+id/menu_fragments"
       android:actionLayout="@layout/fragment"
       android:showAsAction="always">
   </item>

</menu>

我有一个片段如下:

public class ExampleFragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment, container, false);
    return view;
  }
}

我的fragment.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:gravity="center"
     android:orientation="vertical" >

<FrameLayout
    android:id="@+id/fragment_content"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1" />

  <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="0dip"
      android:layout_weight="1"
      android:gravity="center"
      android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        tools:listitem="@android:layout/simple_list_item_1" >
    </ListView>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Button" />
  </LinearLayout>
</LinearLayout>

现在这个问题是,当我调用Fragment时,当我选择键入内容时,fragment.xml中的布局会向上移动。

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

好。没关系。我遇到了问题。

在Actionbar的<requestFocus/> xml文件中的EditText小部件上,这是一个愚蠢的@layout/main。删除它现在有效。

编辑: 对不起,问题仍然存在,但最后我抓住了解决方案。

包括,

android:windowSoftInputMode="adjustPan|adjustResize" 进入您AndroidManifest.xml

中承载片段的活动

如果使用键盘/小键盘,布局不会改变。