java - Android使一个元素只出现在纵向 View 中

标签 java android xml rotation

我有一个支持广告的应用程序。结果,当屏幕旋转时,广告仍然存在。我希望能够让广告只在手机纵向而不是横向时存在。我需要声明一个单独的景观 xml 还是有不同的方法来做到这一点?如果我需要声明一个景观 xml,我该如何在 java 中处理它?<​​br/> 这是相关的 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:screenOrientation="portrait"
    >
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text=" "
        android:id="@+id/textView"
        android:layout_centerHorizontal="true" android:layout_gravity="center_horizontal"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/pic"
                android:layout_weight="1"
        >
    <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"/>


</RelativeLayout>
<com.google.ads.AdView android:id="@+id/adView"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       ads:adUnitId="4"
                       ads:adSize="BANNER"

                       android:layout_centerHorizontal="true"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/pic"
        >
       //a bunch of buttons
</LinearLayout>

谢谢!

更新 这是在实现 layout-land 文件夹并删除我不想要的元素(即 adview)后应用程序崩溃时的 logcat .txt

最佳答案

您需要添加一个文件夹layout-landres 文件夹,并有一个与您希望显示的布局集同名的xml 文件横向。

当方向变为横向时将显示新布局

查看有关支持不同屏幕的文档:

http://developer.android.com/training/basics/supporting-devices/screens.html

另一个可能相关的选项是监视方向变化并通过覆盖 onConfigurationChanged 执行特定的方向变化任务。 有很多关于 SO 的帖子

例如:

Why not use always android:configChanges="keyboardHidden|orientation"?

How to detect orientation change in layout in Android?

希望这有助于...

关于java - Android使一个元素只出现在纵向 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23712950/

相关文章:

java - 如何从 docker-compose 文件覆盖/etc/resolv.conf 中的 Docker 嵌入式 DNS 服务器

java - 如何在锁屏 Activity 中防止电源菜单选项?

xml - 将 XML 文件转换为 CSV

java - JAXB 将 1 个 XML 标记映射到 2 个变量

java - 在 Java 中使用 3 维数组的卡片组

java - 如何创建中间带有单个边框的相邻 Material 按钮

android - 使用publishToMavenLocal创建远程Maven存储库的本地副本

android - 从python输入adb shell

xml - 找不到 Magento Helper 类错误

java - Android:在运行时在java代码中创建radioButtons