使用回收视图时无法在Android模拟器中看到应用栏

贵有恒,何必三更起、五更眠、最无益,只怕一日曝、十日寒。这篇文章主要讲述使用回收视图时无法在Android模拟器中看到应用栏相关的知识,希望能为你提供帮助。
我正在尝试使用android RecyclerView设置Fragments。我无法在我的片段中看到应用栏。作为概念证明,我创建了100个POJOsthat I want to display in myRecyclerView`。我能够看到100个元素,但我无法查看应用栏。
我不知道我做错了什么。这是在模拟器中呈现的视图。我使用的是标准活动API,而不是支持库中的API。

使用回收视图时无法在Android模拟器中看到应用栏

文章图片

【使用回收视图时无法在Android模拟器中看到应用栏】这是我使用布局检查器的视图。
使用回收视图时无法在Android模拟器中看到应用栏

文章图片

主题
< !-- Base application theme. --> < style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> < !-- Customize your theme here. --> < item name="colorPrimary"> @color/colorPrimary< /item> < item name="colorPrimaryDark"> @color/colorPrimaryDark< /item> < item name="colorAccent"> @color/colorAccent< /item> < /style>

Android Manifest.xml
< ?xml version="1.0" encoding="utf-8"?> < manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.android.fragments"> < application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> < activity android:name=".CrimeListActivity"> < intent-filter> < action android:name="android.intent.action.MAIN" /> < category android:name="android.intent.category.LAUNCHER" /> < /intent-filter> < /activity> < /application> < /manifest>

activity.java
public class CrimeListActivity extends Activity {@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); FragmentManager fm = getFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragment_container); if (fragment == null) { fragment = new CrimeListFragment(); fm.beginTransaction().add(R.id.fragment_container, fragment).commit(); } } }

activity_fragment.xml(Recycler View将添加到此容器中)
< ?xml version="1.0" encoding="utf-8"?> < FrameLayout 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" android:id="@+id/fragment_container"> < /FrameLayout>

fragment_crime_list.xml
< ?xml version="1.0" encoding="utf-8"?> < android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/crime_recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingTop="?attr/actionBarSize"/>

list_item_crime.xml
< ?xml version="1.0" encoding="utf-8"?> < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp"> < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/crime_title" android:text="@string/crime_title_hint" /> < TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/crime_date" android:text="@string/crime_date"/> < /LinearLayout>

PO Jo.Java
public class Crime {private final UUID mId; private String mTitle; private final Date mDate; private boolean mSolved; public Crime() { this.mId = UUID.randomUUID(); this.mDate = new Date(); }public UUID getmId() { return mId; }public String getmTitle() { return mTitle; }public void setmTitle(String mTitle) { this.mTitle = mTitle; }public Date getmDate() { return mDate; }public boolean ismSolved() { return mSolved; }public void setmSolved(boolean mSolved) { this.mSolved = mSolved; } }

查看Holder.java
private static class CrimeHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ private TextView mCrimeTitle; private TextView mCrimeDate; private Crime mCrime; public CrimeHolder(View view) { super(view); this.mCrimeTitle = view.findViewById(R.id.crime_title); this.mCrimeDate = view.findViewById(R.id.crime_date); this.itemView.setOnClickListener(this); }@Override public void onClick(View view) { Toast.makeText(view.getContext(), mCrime.getmTitle() + " clicked!!", Toast.LENGTH_SHORT).show(); }void bind(Crime crime) { mCrime = crime; mCrimeTitle.setText(mCrime.getmTitle()); mCrimeDate.setText(mCrime.getmDate().toString()); } }

adapter.Java
private static class CrimeAdapter extends RecyclerView.Adapter< CrimeHolder> {private List< Crime> mCrimes; public CrimeAdapter(List< Crime> crimes) { this.mCrimes = crimes; }@NonNull @Override public CrimeHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) { LayoutInflater layoutInflater = LayoutInflater.from(viewGroup.getContext()); View view = layoutInflater.inflate(R.layout.list_item_crime, viewGroup, false); return new CrimeHolder(view); }@Override public void onBindViewHolder(@NonNull CrimeHolder crimeHolder, int i) { Crime crime = this.mCrimes.get(i); crimeHolder.bind(crime); }@Override public int getItemCount() { return mCrimes.size(); } }

分段
public class CrimeListFragment extends Fragment {private RecyclerView mRecyclerView; private CrimeAdapter mAdapter; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_crime_list, container, false); mRecyclerView = view.findViewById(R.id.crime_recycler_view); mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); updateUI(); return view; }private List< Crime> getCrimes() {List< Crime> mCrimes = new ArrayList< > (); for (int i = 0; i < 100; i++) { Crime crime = new Crime(); crime.setmTitle("Title # :" + i); crime.setmSolved(i % 2 == 0); mCrimes.add(crime); } return mCrimes; }private void updateUI() { List< Crime> crimes = getCrimes(); mAdapter = new CrimeAdapter(crimes); mRecyclerView.setAdapter(mAdapter); } }

答案如果您将主Activity更改为AppCompatActivity,您将获得操作栏,并且您将不需要fragment_crime_list.xml中的paddingTop。我猜测问题是你将一个普通的Activity与一个AppCompat主题结合起来。
(我尝试过这个)
使用回收视图时无法在Android模拟器中看到应用栏

文章图片

另一答案即使您使用DarkActionBar作为父主题,我也不知道为什么它不会出现在您的应用中。但您可以使用this guide手动添加应用栏。
你需要:
修改您的清单,以确保Android不会为您制作应用程序栏
< application android:theme="@style/Theme.AppCompat.Light.NoActionBar" ...

然后在布局中添加工具栏小部件
< android.support.v7.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

然后在你的活动课上
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fragment); Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar); }


    推荐阅读