繁体   English   中英

为什么 Activity 在 Android Studio 预览中的外观与设备上的不同?

[英]Why is the look of the activity different in the Android Studio preview than on the device?

我创建了一个导航抽屉活动,发现它的内容页面在预览中看起来不同。

这是预览。

这是 AVD 的外观(它看起来也像一个真正的设备)。

XML 代码是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.xy.xy.HomePage">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="160sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="5sp">

    <ImageView
        android:id="@+id/TestMenuImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/app_name"
        app:srcCompat="@drawable/bg1"
        tools:scaleType="centerCrop" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        tools:background="@color/hbm_text_background">

    <TextView
        android:id="@+id/TestMenuName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5sp"
        android:contentDescription="@string/app_name"
        android:text="@string/test"
        android:textColor="@color/white" />

    </RelativeLayout>

    <Button
        android:id="@+id/TestMenuButton"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </RelativeLayout>

</LinearLayout>

你觉得问题出在哪里?

编辑:我看到您已经包含样式文件,并且看来您的应用程序正在运行与预览相同的样式(AppTheme),所以我认为我们可以排除这是罪魁祸首。

我怀疑这会有所不同,但我注意到您的预览版正在运行api 27,而您的设备正在运行api 27。

尝试删除tools:context="com.xy.xy.HomePage" ,看看它是否tools:context="com.xy.xy.HomePage"


预览只能向您显示当前视图。 它不知道父视图。 子视图可能具有限制子视图大小的父视图 假设您的图片设置为width =“ match_parent”。 在这种情况下,预览可以扩展图像以填满整个宽度,但是在运行时,您可以将图像放置在宽度为“ 100dp”的父级内部,这将导致图像的宽度仅为100dp。 父视图的填充和边距也会引起问题。

如果这不是问题,那么可能是样式问题。 为了确定,我必须查看更多的主题和样式设置。 您的预览可能会向您显示与您在运行时实际使用的样式/主题不同的样式/主题。

尝试像这样在ImageView中添加android:scaleType="fitXY"

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    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:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.xy.xy.HomePage">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="160sp"
        android:layout_marginLeft="5sp"
        android:layout_marginRight="5sp"
        android:layout_marginTop="5sp">

    <ImageView
        android:id="@+id/TestMenuImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@string/app_name"
        app:srcCompat="@drawable/bg1"
        android:scaleType="fitXY"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        tools:background="@color/hbm_text_background">

    <TextView
        android:id="@+id/TestMenuName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5sp"
        android:contentDescription="@string/app_name"
        android:text="@string/test"
        android:textColor="@color/white" />

    </RelativeLayout>

    <Button
        android:id="@+id/TestMenuButton"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    </RelativeLayout>

</LinearLayout>

这可能是Android Studio中ImageViews预览的一个小错误,

希望能帮助到你

“ styles.xml”文件:

<resources>

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

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

“ AndroidManifest.xml”文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xy.xy">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".HomePage"
        android:label="@string/title_activity_home_page"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.wordwave.wordwave.MainActivity" />
    </activity>
</application>

</manifest>

“ MainActivity.kt”文件:

package com.xy.xy

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM