【问题标题】:Android fragment layout issueAndroid片段布局问题
【发布时间】:2013-11-01 06:14:18
【问题描述】:

在我的 android 应用程序中,我正在从另一个 Sherlock 片段中替换 Sherlock 片段。 (假设 B 中的 A)我使用以下代码进行此替换

   FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
   Fragment fragment = new AddNewRaveFragment();
   ft.replace(R.id.raves_frame, fragment,"NewFragmentTag");
   ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
   ft.addToBackStack(null);
   ft.commit();

我用于片段 A 的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:id="@+id/raves_frame"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="114dp"
    android:layout_marginTop="69dp" >
</FrameLayout>


</RelativeLayout>

但是当我运行程序时,它显示布局像这样向左移动

这是我用于布局 B 的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />
    </EditText>

</LinearLayout>

那么我怎样才能让它在屏幕上正确显示布局呢? 谢谢

【问题讨论】:

  • 删除布局中的 android:layout_alignParentLeft="true"
  • 这是因为你已经把 android:layout_marginLeft 和 android:layout_marginTop 给你的 FrameLayout..

标签: android android-layout android-intent android-listview android-fragments


【解决方案1】:

问题是因为您在FrameLayout 中留有边距。只需删除此行android:layout_marginLeft="114dp" 并删除顶部边距android:layout_marginTop="69dp" 这会留下额外的空间。

【讨论】:

  • 是的,解决了问题,非常感谢您的回答,只是另一个问题,当我将按钮添加到布局 A 时,即使我将其替换为布局 B,按钮仍位于顶部布局的编辑文本 B. 如何解决这个问题?
  • 你能发布你替换布局 B 的代码吗?我猜这个问题可能是因为布局 A 仍然存在。您可能没有清除片段。
  • Gald 来帮助你.. :)
猜你喜欢
  • 2013-01-10
  • 2015-08-13
  • 1970-01-01
  • 1970-01-01
  • 2021-09-26
  • 2013-02-07
  • 2011-07-15
  • 2017-04-15
  • 1970-01-01
相关资源
最近更新 更多