我遇到Theme.AppCompat.Light.NoActionBar
主题的问题,我不确定这是一个错误还是一些不正确的事情。
这就是我定义我的应用主题的方式:
<style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo"></style>
<style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primaryColour</item>
<item name="colorPrimaryDark">@color/primaryDarkColour</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
然后我将其应用于清单中的应用程序,如下所示:
<application
android:name=".MainApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.DesignDemo">
...
</application>
当我将应用程序从IDE直接运行到我的手机或模拟器并且它有一个操作栏时,这很有效。
但是一旦我生成签名的APK;应用程序丢失了操作栏。
知道我在这里做错了什么吗?
这是我的活动:
public class MainActivity extends AppCompatActivity
implements
NewsFragment.OnTenderItemSelectedListener,
ProjectFragment.OnProjectItemSelectedListener,
JobsFragment.Callbacks,
JobsFragment.OnJobSelectedListener,
PublicTenderFragment.OnTenderItemSelectedListener,
PrivateSectorTendersFragment.OnTenderSelectedListener,
CountyDocsFragment.Callbacks, BlogsFragment.OnBlogItemSelectedListener,
MyCountyInfoFragment.Callbacks, PollsFragment.OnPollSelectedListener, NavigationView.OnNavigationItemSelectedListener {
private Handler handler;
private static final String STATE_KEY_COUNTY = "com.governanceapp.STATE_KEY_COUNTY";
private static final String TAG = "MainActivity";
public List<String> mListNameItem;
private ArrayList<FilterSpinner> mCounties = new ArrayList<FilterSpinner>();
private ArrayList<FilterSpinner> mSectors = new ArrayList<FilterSpinner>();
private int mSelectedCounty = 0;
private int mSelectedSector = 0;
private int mSelectedCountyInfoType = 1;
private String mCountyToRestore = "";
// custom configurations
private long mBackPressed;
private static final int TIME_INTERVAL = 2000; // # milliseconds, desired
private static final int NAVDRAWER_CLOSE_DELAY = 250;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
mCountyToRestore = getIntent().hasExtra(STATE_KEY_COUNTY) ? savedInstanceState
.getString(STATE_KEY_COUNTY) : "";
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
handler = new Handler();
/**
* start with the news menu
*/
displayView(R.id.nav_news);
}
public void displayView(int viewId) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (viewId) {
case R.id.nav_home:
fragment = new GetStartedFragment();
title = "Home";
break;
case R.id.nav_county_assembly:
fragment = new MyCountyInfoFragment();
title = "My County Info";
break;
case R.id.nav_news:
fragment = new NewsListFragment();
title = "News";
break;
case R.id.nav_development_projects:
fragment = new ProjectsListFragment();
title = "County Development Projects";
break;
case R.id.nav_jobs:
fragment = new JobsFragment();
title = "Jobs";
break;
case R.id.nav_public_tenders:
fragment = new PublicTendersListFragment();
title = "Public Sector Tenders";
break;
case R.id.nav_private_tenders:
fragment = new PrivateSectorTendersFragment().newInstance(13);
title = "Private Sectore Tenders";
break;
case R.id.nav_public_blogs:
fragment = new BlogsFragment();
title = "Blogs";
break;
case R.id.nav_county_docs:
fragment = new CountyDocsFragment();
title = "County Documents";
break;
case R.id.nav_polls:
fragment = new PollsFragment();
title = "Polls";
break;
case R.id.nav_faq_and_feedback:
startActivity(new Intent(this, HelpActivity.class));
break;
case R.id.nav_contacts:
fragment = new Contacts();
title = "Contacts";
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
// set the toolbar title
if (getSupportActionBar() != null) {
getSupportActionBar().setSubtitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@Override
public void onItemSelected(int itemId, int tagId, int countyId) {
Intent slideintent = new Intent(getApplicationContext(),
NewsSlidePagerActivity.class);
slideintent.putExtra(NewsSlidePagerActivity.ARG_ITEM_ID, itemId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_TAG_ID, tagId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_COUNTY_ID, countyId);
startActivity(slideintent);
}
@Override
public void onAvailableCountiesChanged(ArrayList<FilterSpinner> counties) {
Log.d(TAG, "Got list of available video topics, " + counties.size()
+ " items.");
// make a sorted list of topics
ArrayList<FilterSpinner> sortedTopics = new ArrayList<FilterSpinner>();
sortedTopics.addAll(counties);
Collections.sort(sortedTopics, null);
Spinner topicsSpinner = (Spinner) findViewById(R.id.news_filter_spinner_county);
mCounties.clear();
if (topicsSpinner != null) {
ArrayList<FilterSpinner> items2 = new ArrayList<FilterSpinner>();
int positionToSelect = -1;
items2.add(new FilterSpinner(0, getString(R.string.all_counties)));
mCounties.add(new FilterSpinner(0, "")); // represents
for (int i = 0; i < sortedTopics.size(); i++) {
items2.add(new FilterSpinner(sortedTopics.get(i).getItemId(),
sortedTopics.get(i).getItemName()));
mCounties.add(new FilterSpinner(
sortedTopics.get(i).getItemId(), sortedTopics.get(i)
.getItemName()));
}
populateSpinner(topicsSpinner, items2);
topicsSpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView,
View view, int position, long id) {
if (position >= 0 && position < mCounties.size()) {
onCountySelected(mCounties.get(position)
.getItemId());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
if (positionToSelect > 0) {
topicsSpinner.setSelection(positionToSelect);
mCountyToRestore = "";
}
} else {
// should not happen...
Log.d(TAG,
"Topics spinner not found (Activity not initialized yet?).");
}
}
@Override
public void onAvailableSectorsChanged(ArrayList<FilterSpinner> sectors) {
ArrayList<FilterSpinner> sortedSectors = new ArrayList<FilterSpinner>();
sortedSectors.addAll(sectors);
Collections.sort(sectors);
Spinner sectorsSpinner = (Spinner) findViewById(R.id.news_filter_spinner_sector);
mSectors.clear();
if (sectorsSpinner != null) {
ArrayList<FilterSpinner> sectorItems = new ArrayList<FilterSpinner>();
int positionToSelect = -1;
for (int i = 0; i < sortedSectors.size(); i++) {
sectorItems.add(new FilterSpinner(sortedSectors.get(i)
.getItemId(), sortedSectors.get(i).getItemName()));
mSectors.add(new FilterSpinner(
sortedSectors.get(i).getItemId(), sortedSectors.get(i)
.getItemName()));
}
populateSpinner(sectorsSpinner, sectorItems);
sectorsSpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
onSectorSelected(mSectors.get(position).getItemId());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
private void populateSpinner(Spinner spinner, ArrayList<FilterSpinner> items) {
MySpinnerAdapter adapter1 = new MySpinnerAdapter(
getApplicationContext(), 0, items);
spinner.setAdapter(adapter1);
}
private void onCountySelected(int county) {
if (mSelectedCounty == county) {
return;
}
Log.d(TAG, "County selected: " + county);
JobsFragment frag = (JobsFragment) getSupportFragmentManager()
.findFragmentById(R.id.content_frame);
if (frag == null) {
Log.e(TAG, "PostsListFragment fragment not found.");
return;
}
mSelectedCounty = county;
frag.setFilterAndReload(mSelectedCounty, mSelectedSector);
}
private void onSectorSelected(int sector) {
if (mSelectedSector == sector) {
return;
}
Log.d(TAG, "Sector selected: " + sector);
JobsFragment frag = (JobsFragment) getSupportFragmentManager()
.findFragmentById(R.id.content_frame);
if (frag == null) {
Log.e(TAG, "PostsListFragment fragment not found.");
return;
}
mSelectedSector = sector;
frag.setFilterAndReload(mSelectedCounty, mSelectedSector);
}
@Override
public void onJobSelected(int itemId, int countyId, int tagId, String title) {
Intent slideintent = new Intent(getApplicationContext(),
JobsSlidePagerActivity.class);
slideintent.putExtra(NewsSlidePagerActivity.ARG_ITEM_ID, itemId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_TAG_ID, tagId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_COUNTY_ID, countyId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_ITEM_TITLE, title);
startActivity(slideintent);
}
@Override
public void onTenderItemSelected(int itemId, int tagId, int countyId,
String title) {
Intent slideintent = new Intent(getApplicationContext(),
NewsSlidePagerActivity.class);
slideintent.putExtra(NewsSlidePagerActivity.ARG_ITEM_ID, itemId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_TAG_ID, tagId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_COUNTY_ID, countyId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_ITEM_TITLE, title);
startActivity(slideintent);
}
@Override
public void onAvailableCountyDocsCountiesChanged(
ArrayList<FilterSpinner> counties) {
Log.d(TAG, "Got list of available video topics, " + counties.size()
+ " items.");
// make a sorted list of topics
ArrayList<FilterSpinner> sortedTopics = new ArrayList<FilterSpinner>();
sortedTopics.addAll(counties);
Collections.sort(sortedTopics, null);
Spinner topicsSpinner = (Spinner) findViewById(R.id.county_docs_county_filter);
mCounties.clear();
if (topicsSpinner != null) {
ArrayList<FilterSpinner> items2 = new ArrayList<FilterSpinner>();
int positionToSelect = -1;
items2.add(new FilterSpinner(0, getString(R.string.all_counties)));
mCounties.add(new FilterSpinner(0, "")); // represents
// "All counties"
for (int i = 0; i < sortedTopics.size(); i++) {
items2.add(new FilterSpinner(sortedTopics.get(i).getItemId(),
sortedTopics.get(i).getItemName()));
mCounties.add(new FilterSpinner(
sortedTopics.get(i).getItemId(), sortedTopics.get(i)
.getItemName()));
}
populateSpinner(topicsSpinner, items2);
topicsSpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView,
View view, int position, long id) {
if (position >= 0 && position < mCounties.size()) {
onDocsCountyCountySelected(mCounties.get(
position).getItemId());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
if (positionToSelect > 0) {
topicsSpinner.setSelection(positionToSelect);
mCountyToRestore = "";
}
} else {
// should not happen...
Log.d(TAG,
"Topics spinner not found (Activity not initialized yet?).");
}
}
private void onDocsCountyCountySelected(int county) {
if (mSelectedCounty == county) {
return;
}
Log.d(TAG, "County selected: " + county);
CountyDocsFragment frag = (CountyDocsFragment) getSupportFragmentManager()
.findFragmentById(R.id.content_frame);
if (frag == null) {
Log.e(TAG, "CountyDocsFragment fragment not found.");
return;
}
mSelectedCounty = county;
frag.setFilterAndReload(mSelectedCounty);
}
@Override
public void onBlogItemSelected(int itemId, int tagId, int countyId) {
Intent slideintent = new Intent(getApplicationContext(),
NewsSlidePagerActivity.class);
slideintent.putExtra(NewsSlidePagerActivity.ARG_ITEM_ID, itemId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_TAG_ID, tagId);
slideintent.putExtra(NewsSlidePagerActivity.ARG_COUNTY_ID, countyId);
startActivity(slideintent);
}
@Override
public void onAvailableMyCountyChanged(ArrayList<FilterSpinner> counties) {
// make a sorted list of topics
ArrayList<FilterSpinner> sortedTopics = new ArrayList<FilterSpinner>();
sortedTopics.addAll(counties);
Collections.sort(sortedTopics, null);
Spinner topicsSpinner = (Spinner) findViewById(R.id.my_county_info_county_filter);
mCounties.clear();
if (topicsSpinner != null) {
ArrayList<FilterSpinner> items2 = new ArrayList<FilterSpinner>();
int positionToSelect = -1;
for (int i = 0; i < sortedTopics.size(); i++) {
items2.add(new FilterSpinner(sortedTopics.get(i).getItemId(),
sortedTopics.get(i).getItemName()));
mCounties.add(new FilterSpinner(
sortedTopics.get(i).getItemId(), sortedTopics.get(i)
.getItemName()));
}
populateSpinner(topicsSpinner, items2);
topicsSpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView,
View view, int position, long id) {
if (position >= 0 && position < mCounties.size()) {
onMyCountyInfoSelected(mCounties.get(position)
.getItemId());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
if (positionToSelect > 0) {
topicsSpinner.setSelection(positionToSelect);
mCountyToRestore = "";
}
} else {
// should not happen...
Log.d(TAG,
"Topics spinner not found (Activity not initialized yet?).");
}
}
@Override
public void onAvailableCountyInfoChanged(
ArrayList<FilterSpinner> countyinfotypes) {
// make a sorted list of topics
ArrayList<FilterSpinner> sortedTopics = new ArrayList<>();
sortedTopics.addAll(countyinfotypes);
Collections.sort(sortedTopics, null);
Spinner topicsSpinner = (Spinner) findViewById(R.id.my_county_info_county_info_types_filter);
mSectors.clear();
if (topicsSpinner != null) {
ArrayList<FilterSpinner> items2 = new ArrayList<>();
int positionToSelect = -1;
for (int i = 0; i < sortedTopics.size(); i++) {
items2.add(new FilterSpinner(sortedTopics.get(i).getItemId(),
sortedTopics.get(i).getItemName()));
mSectors.add(new FilterSpinner(sortedTopics.get(i).getItemId(),
sortedTopics.get(i).getItemName()));
}
populateSpinner(topicsSpinner, items2);
topicsSpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView,
View view, int position, long id) {
if (position >= 0 && position < mSectors.size()) {
onMyCountyInfoTypesSelected(mSectors.get(
position).getItemId());
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
if (positionToSelect > 0) {
topicsSpinner.setSelection(positionToSelect);
mCountyToRestore = "";
}
} else {
// should not happen...
Log.d(TAG,
"Topics spinner not found (Activity not initialized yet?).");
}
}
private void onMyCountyInfoSelected(int county) {
if (mSelectedCounty == county) {
return;
}
Log.d(TAG, "County selected:MyCountyInfoFragment: " + county);
MyCountyInfoFragment frag = (MyCountyInfoFragment) getSupportFragmentManager()
.findFragmentById(R.id.content_frame);
if (frag == null) {
Log.e(TAG, "MyCountyInfoFragment fragment not found.");
return;
}
mSelectedCounty = county;
frag.setFilterAndReload(mSelectedCounty, mSelectedCountyInfoType);
}
private void onMyCountyInfoTypesSelected(int info_type) {
if (mSelectedCountyInfoType == info_type) {
return;
}
Log.d(TAG, "Sector selected:MyCountyInfoFragment: " + info_type);
MyCountyInfoFragment frag = (MyCountyInfoFragment) getSupportFragmentManager()
.findFragmentById(R.id.content_frame);
if (frag == null) {
Log.e(TAG, "MyCountyInfoFragment fragment not found.");
return;
}
mSelectedCountyInfoType = info_type;
frag.setFilterAndReload(mSelectedCounty, mSelectedCountyInfoType);
}
@Override
public void onPollSelected(String pollUri) {
Intent slideintent = new Intent(getApplicationContext(),
ViewPollActivity.class);
slideintent.putExtra(ViewPollActivity.POLL_URI, pollUri);
startActivity(slideintent);
}
@Override
public boolean onNavigationItemSelected(final MenuItem item) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
displayView(item.getItemId());
}
}, NAVDRAWER_CLOSE_DELAY);
return true;
}
@Override
public void onBackPressed() {
if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
super.onBackPressed();
return;
} else {
Toast.makeText(getBaseContext(), "Press back again to exit",
Toast.LENGTH_SHORT).show();
}
mBackPressed = System.currentTimeMillis();
}}
包含工具栏的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<!--<android.support.design.widget.FloatingActionButton-->
<!--android:id="@+id/fab"-->
<!--android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_gravity="bottom|end"-->
<!--android:layout_margin="@dimen/fab_margin"-->
<!--android:src="@android:drawable/ic_dialog_email" />-->
</android.support.design.widget.CoordinatorLayout>
其他活动的主题:
<style name="DesignDemo" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/primaryColour </item>
<item name="colorPrimaryDark">@color/primaryDarkColour </item>
<item name="android:windowContentOverlay">@null</item>
<item name="elevation">0dp</item>
</style>
答案 0 :(得分:0)
我设法找到了我面临的上述问题的解决方案......我用Android Studio默认替换了我的样式定义
在styles.xml中
<!-- 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>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
然后在styles.xml(v21)
中<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
我从Eclipse导入了这个项目,我试图像在Eclipse中一样使用这些样式。
一切都好了。