[英]Fragment wont show in activity
我對android開發非常陌生。 我瀏覽了一些有關片段的教程,但是由於某種原因,我似乎無法從主視圖中添加片段。 我沒有嘗試做任何痛苦的事情。 我要做的只是創建片段並將其添加到我的主要活動中。 而已。 但是,每次運行應用程序時,我得到的都是空白屏幕。 我對其進行調試,然后將其片段化。 這使我相信問題可能出在xml laout文件中(也許)。
這是我的MainActivity.java
package com.android.myCompany.nameOfApp;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.foo_frame_layout, new FooFragment());
ft.commit();
}
}
這是我的Fragment.cs
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link Fragment} subclass.
*/
public class FooFragment extends Fragment {
public FooFragment () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText(R.string.hello_blank_fragment);
return textView;
}
}
這是我的布局activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/background_portrait">
<FrameLayout
android:id="@+id/copyright_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
這是我的fragment_foo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/copyright_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FooFragment">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="102dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="false"
android:cropToPadding="false"
app:layout_constraintBottom_toTopOf="@+id/imageView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
<ImageView
</RelativeLayout>
誰能告訴我我的設置有什么問題嗎? 提前謝謝了。
在您的片段類中對此進行更改:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_foo, container, false); // This line will inflate your fragment_foo layout and return it's rootview for fragment inflation
}
然后在片段的onViewCreated
方法中找到您的片段視圖。
嘗試將其添加到您的片段類中...
public static FooFragment newInstance()
{
return new FooFragment();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_layout, container, false);
// work with your ui code here.
return view;
}
並在您的活動中調用此方法...
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.foo_frame_layout, FooFragment.newInstance());
ft.commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.