我差不多完成了一个应用程序,我使用底部导航栏在不同的布局(不是活动,如果相关)之间切换,以及一些自定义布局,用于手机和平板电脑的横向。当我在模拟器中运行它时,我的应用程序按预期运行,但是当我导出apk以在我的设备上测试时(在这种情况下是手机),应用程序根本不会切换到横向模式。
如果您希望我粘贴我认为相关的代码的某些部分,请告诉我 - 我不知道从哪里开始解决这个问题。
编辑:以下是清单和活动文件:
清单:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".StoryListActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".StoryDetailActivity"
android:label="@string/title_story_detail"
android:parentActivityName=".StoryListActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.compy.someappname.StoryListActivity" />
</activity>
</application>
的活动:
公共类StoryListActivity扩展了AppCompatActivity {
private FrameLayout mainFrameLayout;
private FrameLayout shopFrameLayout;
private FrameLayout infoFrameLayout;
/**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device.
*/
private boolean mTwoPane;
private int visiblePart;
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
if (mainFrameLayout.getVisibility() == View.VISIBLE) {
savedInstanceState.putInt("visiblePart", 0);
} else if (shopFrameLayout.getVisibility() == View.VISIBLE) {
savedInstanceState.putInt("visiblePart", 1);
} else if (infoFrameLayout.getVisibility() == View.VISIBLE) {
savedInstanceState.putInt("visiblePart", 2);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null){
visiblePart = savedInstanceState.getInt("visiblePart");
}
setContentView(R.layout.activity_story_list);
mainFrameLayout = (FrameLayout) findViewById(R.id.mainFrameLayout);
shopFrameLayout = (FrameLayout) findViewById(R.id.shopFrameLayout);
infoFrameLayout = (FrameLayout) findViewById(R.id.infoFrameLayout);
appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
TextView titleTextView = (TextView) findViewById(R.id.titleTextView);
TextView titleTextView2 = (TextView) findViewById(R.id.titleTextView2);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Ovo-Regular.ttf");
titleTextView.setTypeface(typeface);
titleTextView2.setTypeface(typeface);
if (visiblePart == 0) {
mainFrameLayout.setVisibility(View.VISIBLE);
shopFrameLayout.setVisibility(View.GONE);
infoFrameLayout.setVisibility(View.GONE);
} else if (visiblePart == 1) {
mainFrameLayout.setVisibility(View.GONE);
shopFrameLayout.setVisibility(View.VISIBLE);
infoFrameLayout.setVisibility(View.GONE);
} else if (visiblePart == 2) {
mainFrameLayout.setVisibility(View.GONE);
shopFrameLayout.setVisibility(View.GONE);
infoFrameLayout.setVisibility(View.VISIBLE);
}
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_main:
mainFrameLayout.setVisibility(VISIBLE);
shopFrameLayout.setVisibility(GONE);
infoFrameLayout.setVisibility(GONE);
break;
case R.id.action_shop:
mainFrameLayout.setVisibility(GONE);
shopFrameLayout.setVisibility(VISIBLE);
infoFrameLayout.setVisibility(GONE);
break;
case R.id.action_info:
mainFrameLayout.setVisibility(GONE);
shopFrameLayout.setVisibility(GONE);
infoFrameLayout.setVisibility(VISIBLE);
break;
}
return false;
}
});
View recyclerView = findViewById(R.id.story_list);
assert recyclerView != null;
setupRecyclerView((RecyclerView) recyclerView);
if (findViewById(R.id.story_detail_container) != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
mTwoPane = true;
}
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(DummyContent.ITEMS));
}
public class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
private final List<DummyContent.DummyItem> mValues;
public SimpleItemRecyclerViewAdapter(List<DummyContent.DummyItem> items) {
mValues = items;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.story_list_content, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).id);
holder.mContentView.setText(mValues.get(position).story_title);
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(StoryDetailFragment.ARG_ITEM_ID, holder.mItem.id);
StoryDetailFragment fragment = new StoryDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.story_detail_container, fragment)
.commit();
} else {
Context context = v.getContext();
Intent intent = new Intent(context, StoryDetailActivity.class);
intent.putExtra(StoryDetailFragment.ARG_ITEM_ID, holder.mItem.id);
context.startActivity(intent);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
public DummyContent.DummyItem mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.id);
mContentView = (TextView) view.findViewById(R.id.content);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Ovo-Regular.ttf");
mIdView.setTypeface(typeface);
mContentView.setTypeface(typeface);
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
}
答案 0 :(得分:0)
你可以试试这个
/**
* Getting screen orientation and ON rotation if it is OFF
*/
private void rotationON() {
if (Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 0) {
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
}
}
/**
* Getting screen orientation and Off rotation if it is ON
*/
private void rotationOFF() {
if (Settings.System.getInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0) == 1) {
Settings.System.putInt(getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 0);
}
}