我正在制作 Udacity Sunshine 应用程序。因为以前使用的android studio版本是0.5.8,现在我使用的是android studio 3.0.1。因此,这两者中使用的编码技术之间存在很大差距。 因此,在这里我在空 Activity 中使用了 fragment ,并且通过阅读developer.android.com上有关 fragment 的文章来实现它,我得到了关于它的抽象想法,但其中很多内容仍然不清楚。 所以我完成了添加 fragment 并向 ListView 添加假数据以填充它。 但是当我在设备上运行该应用程序时,它仍然显示空白屏幕,并且没有我在代码中添加的 TextView :
布局/activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" />
布局/fragment_layout.xml
<FrameLayout 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"
tools:context=".ArticleFragment"
tools:showIn="@layout/activity_main">
<ListView
android:id="@+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
布局/list_of_item_forecast.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:id="@+id/list_item_forecast_textview"
tools:layout="@layout/fragment_layout"/>
包/MainActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
}
包/ArticleFragment.java
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.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArticleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create some dummy data for the ListView. Here's a sample weekly forecast
String[] data = {
"Mon 6/23 - Sunny - 31/17",
"Tue 6/24 - Foggy - 21/8",
"Wed 6/25 - Cloudy - 22/17",
"Thurs 6/26 - Rainy - 18/11",
"Fri 6/27 - Foggy - 21/10",
"Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
"Sun 6/29 - Sunny - 20/7"
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
// Now that we have some dummy forecast data, create an ArrayAdapter.
// The ArrayAdapter will take data from a source (like our dummy forecast) and
// use it to populate the ListView it's attached to.
ArrayAdapter<String> mForecastAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_forecast, // The name of the layout ID.
R.id.list_item_forecast_textview, // The ID of the textview to populate.
weekForecast);
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listview = (ListView) rootView.findViewById(
R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
// Inflate the layout for this fragment
return rootView;
}
}
最佳答案
您的代码的问题是您没有实例化您创建的 Fragment 类。您需要实例化它并将其传递给 FragmentManager
,并在主 Activity 中更改 setContentView(R.layout.fragment_layout)
。
只需将您的 MainActivity.java
类更改为此即可解决您的问题。无需更改布局文件。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new ArticleFragment())
.commit();
}
}
关于java - 如何添加 fragment 以使我的应用程序不会在屏幕上显示为空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253903/