【问题标题】:Cannot create an AlertDialog that is viewed horizontally on Android无法创建在 Android 上水平查看的 AlertDialog
【发布时间】:2012-01-22 17:07:48
【问题描述】:

我能够创建一个 AlertDialog。问题是我的应用程序都是水平查看的,但 AlertDialog 是垂直显示的。 其他 GUI 部分是使用位图绘制的,这就是为什么我能够使它们水平而不在 main.xml 文件中指定它的原因。 我确实为线性布局添加了水平方向,但它没有帮助。 main.xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="test.app"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TheActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

创建对话框如下所示:

            {
                AboutClick = true;
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                builder.setTitle("Pick a color");
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
                View layout = inflater.inflate(R.layout.main, null);
                builder.setView(layout);
                AboutDialog = builder.create();
                AboutDialog.show();
            }

知道为什么对话框是垂直显示而不是水平显示吗?

【问题讨论】:

  • 您的应用是否识别出您已将手机横置?如果它没有意识到发生了这种情况,那么它仍然认为屏幕是垂直的,并且会垂直显示对话框。
  • 我希望它始终是水平的,无论用户如何握住手机。

标签: android android-alertdialog alignment


【解决方案1】:

如您所愿。要始终保持水平,您需要在 manifest.xml 内的标记中添加 android:screenOrientation="landscape" 作为属性,如下所示:

<activity android:name=".TheActivity"
android:screenOrientation="landscape"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

【讨论】:

  • 谢谢,这正是我必须做的。
猜你喜欢
  • 2023-03-22
  • 2019-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多