将图像填充到全屏,保持纵横比

时间:2018-03-23 11:25:12

标签: android android-layout

我的问题与Full screen background image in an activity相同。我没有使用背景选项,而是使用ImageView选项,因为我想保持纵横比(我不介意图像的某些部分被裁剪)。我认为该解决方案之前有效,但不知怎的,它开始不起作用并显示白色状态栏。 enter image description here

我在上面的问题中尝试了其他的东西,但是我无法得到我想要的结果(状态栏下方的背景图片)。我做错了什么?以下是重要的完整源代码(我排除了样板。)

PS:我想保留状态栏。我想要的是链接问题的屏幕截图:全屏图像和图像上方的半透明状态栏。如果我将背景图像设置为根布局,我会得到我想要的结果,但不保留图像宽高比。见下图。

enter image description here

清单

<application
    android:theme="@style/AppTheme.NoActionBar">
    <activity
        android:name=".MainActivity">
    </activity>
</application>

活动

class MainActivity : AppCompatActivity()
{

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

布局

<android.support.constraint.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:adjustViewBounds="true"
    tools:context=".MainActivity">

    <ImageView
        android:adjustViewBounds="true"
        android:id="@+id/ivBackground"
        android:src="@drawable/doge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:fitsSystemWindows="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteY="0dp"/>
</android.support.constraint.ConstraintLayout>

Style v21

<resources>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
    </style>
</resources>

风格

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="windowActionBarOverlay">true</item>
        <item name="android:windowActionBarOverlay">true</item>
    </style>

</resources>

[已添加]我终于有了它的工作。但这有点奇怪。首先,根必须是CoordinatorLayout。 ConstraintLayout没有用。其次,必须是如下的虚拟视图。如果删除了虚拟视图,则状态栏变为白色。 android:statusBarColorandroid:windowTranslucentStatus无关紧要。前者使状态栏透明,后者使其显得阴暗。请参阅下面的屏幕截图。我认为这可能是支持库27.1.0的错误。

这种布局有效。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <View
        android:fitsSystemWindows="true"
        android:layout_width="1dp"
        android:layout_height="1dp"/>

    <ImageView
        android:fitsSystemWindows="true"
        android:id="@+id/ivBackground"
        android:src="@drawable/doge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
</android.support.design.widget.CoordinatorLayout>

enter image description here enter image description here

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,解决方法是将CoordinatorLayout设置为根布局,然后在其中添加以前的布局根。

此外,在Style v21中删除此部分也可能有所帮助:

<item name="android:statusBarColor">@android:color/transparent</item>
相关问题