我有一个支持广告的应用程序。结果,当屏幕旋转时,广告仍然存在。我希望能够让广告只在手机纵向而不是横向时存在。我需要声明一个单独的景观 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-land 到res 文件夹,并有一个与您希望显示的布局集同名的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/