Android Studio,布局不会在模拟器上显示

时间:2022-07-16 18:54:17

I just downloaded the new Android Studio Application and I started the training on their website. I'm on "Building your first Android App" and the "Building a simple user interface" part of it. However, the files they told me to edit was activity_my.xml, but everything was pointing to another file in my layout called content_my.xml. So I edited that file instead and got the design working on it, however when I click back to activity_my.xml, the design is not there at all.

我刚下载了新的Android Studio应用程序,我在他们的网站上开始了培训。我正在“构建您的第一个Android应用程序”和“构建一个简单的用户界面”的一部分。但是,他们告诉我编辑的文件是activity_my.xml,但是所有内容都指向我布局中名为content_my.xml的另一个文件。所以我编辑了那个文件而得到了设计,但是当我点击返回activity_my.xml时,设计根本就不存在了。

Code for activity_my.xml (this was given to me I did not edit it at all)

activity_my.xml的代码(这是给我的,我根本没有编辑它)

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
tools:context=".MyActivity">

<android.support.design.widget.AppBarLayout   
 android:layout_height="wrap_content"
 android:layout_width="match_parent"    
 android:theme="@style/AppTheme.AppBarOverlay">

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
  android:layout_width="match_parent"  
  android:layout_height="?attr/actionBarSize"
  android:background="?attr/colorPrimary" 
  app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_my" />

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

code for content_my.xml (What I edited according to the training)

content_my.xml的代码(我根据培训编辑的内容)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">

<EditText
    android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"  />

For the strings, I have also done what the training had asked:

对于琴弦,我也做了培训所要求的:

<resources>
<string name="app_name">My First App</string>
<string name="action_settings">Settings</string>
<string name="button_send">Send</string>
<string name="edit_message">Enter a message</string>

After I run the app using the emulator I would get this:

在我使用模拟器运行应用程序后,我会得到:

Doesn't run the correct design as viewed on the application

不运行在应用程序上查看的正确设计

I was supposed to get this:

我应该得到这个:

Correct view for emulator

正确的模拟器视图

2 个解决方案

#1


0  

Normally the ActionBar reserves room and pushes content down accordingly, but the new AppBarLayout matched with a ToolBar doesn't do this. This means the other layouts need to be told of the AppBarLayout's existance to correctly position themselves.
Your layout isn't doing this, so the EditText and Button are being hidden by the AppBarLayout.

通常,ActionBar会保留空间并相应地推送内容,但与ToolBar匹配的新AppBarLayout不会这样做。这意味着需要告知其他布局AppBarLayout是否存在正确定位自己的位置。你的布局没有这样做,所以AppBarLayout隐藏了EditText和Button。

Add this to your LinearLayout in content_my.xml

将其添加到content_my.xml中的LinearLayout

app:layout_behavior="@string/appbar_scrolling_view_behavior"

I am 99.9% sure this code is in the default activity when a new project is created, so you must've deleted it when editing the file.

我99.9%确定在创建新项目时此代码是默认活动,因此您在编辑文件时必须删除它。

The standard activity layout is split into 2 parts by default now (you can easily change this back by just copying the contents of content_my.xml into activity_my.xml if you wanted). The guide you're reading is most likely a bit outdated.

默认情况下,标准活动布局现在默认分为两部分(如果需要,只需将content_my.xml的内容复制到activity_my.xml中即可轻松更改)。您正在阅读的指南很可能有点过时。

#2


0  

Normally the ActionBar reserves room and pushes content down accordingly, but the new AppBarLayout matched with a ToolBar doesn't do this. This means the other layouts need to be told of the AppBarLayout's existance to correctly position themselves. Your layout isn't doing this, so the EditText and Button are being hidden by the AppBarLayout.

通常,ActionBar会保留空间并相应地推送内容,但与ToolBar匹配的新AppBarLayout不会这样做。这意味着需要告知其他布局AppBarLayout是否存在正确定位自己的位置。你的布局没有这样做,所以AppBarLayout隐藏了EditText和Button。

Add this to your LinearLayout in content_my.xml

将其添加到content_my.xml中的LinearLayout

#1


0  

Normally the ActionBar reserves room and pushes content down accordingly, but the new AppBarLayout matched with a ToolBar doesn't do this. This means the other layouts need to be told of the AppBarLayout's existance to correctly position themselves.
Your layout isn't doing this, so the EditText and Button are being hidden by the AppBarLayout.

通常,ActionBar会保留空间并相应地推送内容,但与ToolBar匹配的新AppBarLayout不会这样做。这意味着需要告知其他布局AppBarLayout是否存在正确定位自己的位置。你的布局没有这样做,所以AppBarLayout隐藏了EditText和Button。

Add this to your LinearLayout in content_my.xml

将其添加到content_my.xml中的LinearLayout

app:layout_behavior="@string/appbar_scrolling_view_behavior"

I am 99.9% sure this code is in the default activity when a new project is created, so you must've deleted it when editing the file.

我99.9%确定在创建新项目时此代码是默认活动,因此您在编辑文件时必须删除它。

The standard activity layout is split into 2 parts by default now (you can easily change this back by just copying the contents of content_my.xml into activity_my.xml if you wanted). The guide you're reading is most likely a bit outdated.

默认情况下,标准活动布局现在默认分为两部分(如果需要,只需将content_my.xml的内容复制到activity_my.xml中即可轻松更改)。您正在阅读的指南很可能有点过时。

#2


0  

Normally the ActionBar reserves room and pushes content down accordingly, but the new AppBarLayout matched with a ToolBar doesn't do this. This means the other layouts need to be told of the AppBarLayout's existance to correctly position themselves. Your layout isn't doing this, so the EditText and Button are being hidden by the AppBarLayout.

通常,ActionBar会保留空间并相应地推送内容,但与ToolBar匹配的新AppBarLayout不会这样做。这意味着需要告知其他布局AppBarLayout是否存在正确定位自己的位置。你的布局没有这样做,所以AppBarLayout隐藏了EditText和Button。

Add this to your LinearLayout in content_my.xml

将其添加到content_my.xml中的LinearLayout