我對Android非常陌生,我打算將它發佈到Android Developers - Google Groups,但他們似乎說新手需要發佈到Stack Overflow。所以我在這裏。將內容放置在AppBarLayout的CoordinatorLayout中
我1.4.1昨天下載最新Android Studio中的版本,我也跟着上Building Your First App的說明。我所做的一切都是Starting Another Activity。看起來這些說明有點陳舊,即對於之前版本的SDK,因爲它們沒有引用CoordinatorLayout
和AppBarLayout
,儘管它們出現在代碼中,如果您按照步驟操作。很顯然,我在代碼中做了一些小改動,以使這個應用程序可以正常工作,但並不完全。
我的問題:如果你看一下圖片在Starting Another Activity底部你會看到它們都有標題我的第一個應用程序。在我對代碼的修改中,我無法在圖像/屏幕上獲得該標題。 (我要指出,我想用AppBarLayout
和最新版本CoordinatorLayout
)
讓我們關注的第一個屏幕上,在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">
<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" />
</android.support.design.widget.CoordinatorLayout>
截至Building a Simple User Interface的content_my.xml
的底部所提看起來像:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal" >
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
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"
android:onClick="sendMessage"/>
</LinearLayout>
反正是有,我可以AppBarLayout
添加到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="wrap_content"
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" />
</android.support.design.widget.CoordinatorLayout>
這樣做的問題是,在content_my.xml
內容去AppBarLayout
的Toolbar
後面,而不是在它下面。任何想法如何解決這個問題?
對不起,您可以製作tldr嗎? – deadfish
這似乎是2個單獨的問題,至於你的第一個問題,要在工具欄中獲取應用程序標題,一定要調用'setSupportActionBar(toolbar)',其中'toolbar'是對工具欄視圖的引用。此外,「CoordinatorLayout」和「AppBarLayout」沒有被引用的原因是因爲在之前版本的Android Studio中,「空白活動」創建了一個完全空白的活動。它現在用這兩個元素創建一個簡單的佈局。新選項稱爲「空白活動」。 – Orbit