我有一个(希望)一个相对简单的问题.如何告诉Android哪个布局用于纵向以及哪个布局用于我的AppWidget上的横向?
提前致谢!
如何使我的Android应用程序成为横向或纵向,但没有相反的方向?
意思是说我想启用SCREEN_ORIENTATION_PORTRAIT和SCREEN_ORIENTATION_LANDSCAPE,但禁用SCREEN_ORIENTATION_REVERSE_LANDSCAPE和SCREEN_ORIENTATION_REVERSE_PORTRAIT.
谢谢
只想阻止方向,直到我的视图中加载了所有数据.所以我想到了以下代码,但它不起作用.
private class task extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
...
}
protected void onPostExecute(String result) {
...
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
protected void onPreExecute() {
...
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行这两行SENSOR并且NOSENSOR我的屏幕自动变为水平,而不理解为什么.那可能会发生?
编辑: 我将以下行检查当前方向,结果如下:
protected void onPreExecute() {
if (getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE) {
Log.e("TAG","LANDSCAPE");
}else{
Log.e("TAG","PORTRAIT");
}
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
}
Run Code Online (Sandbox Code Playgroud)
Logcat的结果:
LANDSCAPE
Run Code Online (Sandbox Code Playgroud)
但是,如果我删除两行(SetRequestedOrientation),我在logcat中得到这个:
PORTRAIT
Run Code Online (Sandbox Code Playgroud) 我使用MediaPlayer播放MP3.目前我通过使用禁用屏幕方向更改
android:screenOrientation="portrait"
android:configChanges="keyboardHidden|orientation"
Run Code Online (Sandbox Code Playgroud)
在清单中.我现在想要支持横向模式 - 即删除那些标签 - 但是存在的问题是在销毁/创建周期期间玩家停止然后重新启动.这是可以的,我实际上甚至手动执行此操作onPause()以在活动进入后台时停止播放器.
为了使它在方向更改期间保持运行,我尝试将其设置为静态(并使用应用程序上下文创建它一次).当然,当我删除了player.stop()在onPause()现在,但它确实我想要的-嗯,直到活动进入后台.
那么,有两个问题:
onStop()MediaPlayer在该周期内保持运行,但在应用程序进入后台时停止运行?android screen-orientation android-mediaplayer android-activity
我尝试实现以下代码来处理屏幕方向更改.
****DataBaseUpdateService.java****
public class DataBaseUpdateService extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Updatetask Task = new Updatetask(this.getApplicationContext());
if(Task.getStatus() == AsyncTask.Status.PENDING){
Task.execute();};
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
}
Run Code Online (Sandbox Code Playgroud)
================================================== ========================
****Androidmanifest.xml****
<activity
android:name=".DataBaseUpdateService"
android:configChanges="keyboardHidden|orientation"/>
Run Code Online (Sandbox Code Playgroud)
这些代码与android 3.x或更低版本完美配合.但是,它无法正常运行android 4.x.
你知道问题是什么吗?
这里有部分活动屏幕方向改变:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText) findViewById(R.id.editText1);
et.setOnLongClickListener(new View.OnLongClickListener()
{
@Override
public boolean onLongClick(View v)
{
Fragment1 dialogFragment = new Fragment1();
dialogFragment.show(getFragmentManager(), null);
dialogFragment.setTextDialog(et.getText().toString());
return true;
}
});
}
Run Code Online (Sandbox Code Playgroud)
显然,似乎在DialogFragment中出现的对话框应该出现在onLongClick之后的editText上(我知道当屏幕方向改变时Activity重新启动,但它不应该像第一次创建时那样正常启动? )
我的问题:当我至少打开一次对话框并关闭它时,屏幕方向改变之后我再次在屏幕上显示对话框,就像我长按了editText一样.
我不清楚为什么会这样.
我还附加了对话框片段的结构:
public Dialog onCreateDialog(Bundle savedInstanceState)
{
final Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater adbInflater = LayoutInflater.from(getActivity());
View eulaLayout = adbInflater.inflate(R.layout.dialog_crypt, null);
Button btn_OK = (Button) eulaLayout.findViewById(R.id.btnOK);
dialog.setContentView(eulaLayout);
final EditText et = (EditText)eulaLayout.findViewById(R.id.editText2);
et.setText(textDialog);
if(et.length()>0)
{
et.setText(et.getText().toString() + " ");
}
et.setSelection(et.length());
btn_OK.setOnClickListener( …Run Code Online (Sandbox Code Playgroud) 我想在锁定的纵向方向模式中找到相机屏幕方向,我在片段类中使用相机并且我已将屏幕方向设置为肖像,但我面临的问题是,当我将相机从纵向转为横向它变化,我需要设置捕获按钮仅在相机处于纵向模式时可见.任何人都可以帮我在纵向模式下更改方向吗?以下是我的代码:
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
sensorManager.registerListener(new SensorEventListener() {
int orientation=-1;;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.values[1] < 6.5 && event.values[1] > -6.5) {
if (orientation!=1) {
Log.d("Sensor", "Landscape");
}
orientation = 1;
} else {
if (orientation!=0) {
Log.d("Sensor", "Portrait");
}
orientation = 0;
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME);
if (orientation == 0) {
// capture button visisble
} else {
// invisible
}
Run Code Online (Sandbox Code Playgroud) 我将清单集中的活动方向设为肖像.因为我不希望我的屏幕旋转.
但是在单击按钮时,我想在Android中获取当前的设备方向(纵向或横向)?我不需要从传感器不断更新; 当我点击按钮就足够了当前的设备方向.
PS:设备方向是我拿着我的设备的方式.请勿将其与屏幕方向混合使用.
这是我到目前为止做了什么?
int PORT=1,LAND=2;
WindowManager windowService = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
int rotation = windowService.getDefaultDisplay().getRotation();
if (Surface.ROTATION_0 == rotation) {
rotation = PORT;
} else if(Surface.ROTATION_180 == rotation) {
rotation = PORT;
} else if(Surface.ROTATION_90 == rotation) {
rotation = LAND;
} else if(Surface.ROTATION_270 == rotation) {
rotation = LAND;
}
return rotation;
Run Code Online (Sandbox Code Playgroud)
即使使用以下方法也无济于事
getResources().getConfiguration().orientation
Run Code Online (Sandbox Code Playgroud)
它始终返回1,因为我的活动默认设置为纵向.
示例/示例代码将受到高度赞赏.
谢谢.
android screen-orientation device-orientation android-orientation
我收到此错误:
Uncaught (in promise) DOMException: lockOrientation() is not available on this device.
code: 9
message: "lockOrientation() is not available on this device."
name: "NotSupportedError"
Run Code Online (Sandbox Code Playgroud)
当我在Chrome中运行以下代码时:
try {
screen.orientation.lock('portrait');
} catch (error) {
// whatever
}
Run Code Online (Sandbox Code Playgroud)
由于桌面Chrome不支持方向锁定,因此预计会抛出错误.我想捕获错误,所以它不会乱丢控制台,但将它包装在一个try...catch块中似乎不起作用.
为什么我不能抓住它?我错过了什么吗?
今天我想到了一种改进平板电脑设计的方法,我找到了这张图片
我想要那么糟糕,因为它看起来很神奇.我现在正在谷歌搜索大约一个小时,我没有遇到任何好的教程.我找到了这个:v21前棒棒糖的材料设计.
我立即开始实施这一切,我尝试的一切都完全错了.独立操作栏的主题需要是ThemeOverlay.AppCompat.ActionBar我的手机布局,我正在扩展Theme.AppCompat.NoActionBar主题.(主题如下)
目前还不清楚我应该做些什么来制作像平板电脑上面的图像,并在手机上显示普通(自定义)supportActionBar 而不会弄乱其中一个.
这是我的AppTheme风格(我申请我的应用程序)
<!-- Base application theme. -->
<style name="AppTheme" parent="@style/Theme.AppCompat.NoActionBar">
<!-- your app branding color for the app bar -->
<item name="colorPrimary">@color/primaryColor</item>
<!-- darker variant for the status bar and contextual app bars -->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!-- theme UI controls like checkboxes and text fields -->
<item name="colorAccent">@color/colorAccent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在你问之前,是的,我已经在SO上发现了这个问题,但这不是一个重复的问题.Chris Banes所写的帖子也没有让我明白.
是否可以在不破坏布局的情况下执行此操作?大声思考,我选择自定义工具栏的原因是因为我已经包含了自定义搜索视图,但已将其删除.工具栏中还有另一个视图,但我认为如果真的有必要可以将其删除.
这是我手机版的布局.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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/coordinatorLayout"
android:layout_width="match_parent" …Run Code Online (Sandbox Code Playgroud) android tablet screen-orientation android-layout material-design