片段在布局内不可见但工作正常

问题描述:

我已经阅读了许多关于同一问题的问题,但没有一个答案解决了我的问题。片段在布局内不可见但工作正常

这是我活动的布局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:host="clyky.cartracker.activities.RegistrationActivity" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_marginStart="@dimen/container_default_left_margin" 
    android:layout_marginEnd="@dimen/container_default_right_margin" 
    android:layout_marginTop="@dimen/activity_vertical_margin" 
    android:layout_marginBottom="@dimen/activity_vertical_margin"> 

    <FrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginBottom="10dp"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/ic_id_card"/> 
    </FrameLayout> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:id="@+id/txtUsername" 
     android:hint="Username" 
     android:layout_marginBottom="@dimen/edit_text_bottom_margin" 
     android:padding="5dp"/> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword" 
     android:id="@+id/txtPassword" 
     android:hint="Password" 
     android:layout_marginBottom="@dimen/edit_text_bottom_margin" 
     android:padding="5dp"/> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword" 
     android:id="@+id/txtConfirmPassword" 
     android:hint="Confirm password" 
     android:layout_marginBottom="@dimen/edit_text_bottom_margin" 
     android:padding="5dp"/> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" 
     android:id="@+id/txtName" 
     android:hint="Name" 
     android:layout_marginBottom="@dimen/edit_text_bottom_margin" 
     android:padding="5dp"/> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text" 
     android:id="@+id/txtSurname" 
     android:hint="Surname" 
     android:layout_marginBottom="@dimen/edit_text_bottom_margin" 
     android:padding="5dp"/> 

    <Button 
     android:text="Registrati" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/btnSignUp" 
     android:layout_margin="15dp"/> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/registrationContainer"> 
    </FrameLayout> 
</LinearLayout> 

我的片段最后FrameLayoutregistrationContainer内补充说:

LoadingRegistrationFragment f = LoadingRegistrationFragment.newInstance(args); 
Utilities.addFragment(R.id.registrationContainer, f, null, getSupportFragmentManager()); 

这是我addFragment方法:

public static void addFragment(int containerID, Fragment toAdd, @Nullable String tag, FragmentManager manager) 
    { 
     FragmentTransaction trans = manager.beginTransaction(); 
     trans.add(containerID, toAdd, tag); 
     trans.commit(); 
    } 

而且这是我的LoadingRegistrationFragmentonCreateView

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     Bundle args = getArguments(); 
     username = args.get("username").toString(); 
     encryptedPassword = args.get("password").toString(); 
     name = args.get("name").toString(); 
     surname = args.get("surname").toString(); 
     listener = (RegistrationListener) getActivity(); 
     View v = inflater.inflate(com.devspark.progressfragment.R.layout.fragment_progress, container, false); 
     sendRequest(); 

     return v; 
    } 

我的片段工作正常,它只是看不见,我找不出原因。 我用调试器检查过,如果v在我的onCreateViewnull但不是,它已经得到了一个值。 我也尝试将我的活动布局封装在ScrollView中,以查看片段是否显示得太低,但仍然不可见。

+0

为了调试目的,只隐藏所有对象并放置仅framelayout来测试片段是否被加载。增加重量到framelayout。如果可能的话,你可以用pic更新屏幕的加载方式。 –

+0

请继续并分享您的片段XML –

+0

@RedM它是一个外部库,我没有片段XML,但它只是一个循环不确定的进度条。这是图书馆:https://github.com/johnkil/Android-ProgressFragment – Clyky

尝试使用trans.replace(containerID, toAdd)代替trans.add(containerID, toAdd, tag)

而改变属性的片段

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

我已经尝试过,但它不起作用 – Clyky

+0

@Clyky尝试改变FrameLayout属性,我添加回答 –

在您的MainActivity的容器,而不是有这样的:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/registrationContainer"> 
</FrameLayout> 

与此替换它:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/registrationContainer"/>