2015-11-11 97 views
24

我對Android非常陌生,我打算將它發佈到Android Developers - Google Groups,但他們似乎說新手需要發佈到Stack Overflow。所以我在這裏。將內容放置在AppBarLayout的CoordinatorLayout中

我1.4.1昨天下載最新Android Studio中的版本,我也跟着上Building Your First App的說明。我所做的一切都是Starting Another Activity。看起來這些說明有點陳舊,即對於之前版本的SDK,因爲它們沒有引用CoordinatorLayoutAppBarLayout,儘管它們出現在代碼中,如果您按照步驟操作。很顯然,我在代碼中做了一些小改動,以使這個應用程序可以正常工作,但並不完全。

我的問題:如果你看一下圖片在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 Interfacecontent_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內容去AppBarLayoutToolbar後面,而不是在它下面。任何想法如何解決這個問題?

+0

對不起,您可以製作tldr嗎? – deadfish

+1

這似乎是2個單獨的問題,至於你的第一個問題,要在工具欄中獲取應用程序標題,一定要調用'setSupportActionBar(toolbar)',其中'toolbar'是對工具欄視圖的引用。此外,「CoordinatorLayout」和「AppBarLayout」沒有被引用的原因是因爲在之前版本的Android Studio中,「空白活動」創建了一個完全空白的活動。它現在用這兩個元素創建一個簡單的佈局。新選項稱爲「空白活動」。 – Orbit

回答

63

CoordinatorLayout中的佈局需要定義layout_behavior。將您的內容更改爲:

<?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="horizontal" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
> 

<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> 
+0

完美!非常感謝你。我知道這可能聽起來很愚蠢,但我試圖谷歌'app:layout_behavior =「@ string/appbar_scrolling_view_behavior」',我找不到任何文檔。我很感激有人發佈了記錄這個URL的地址。 –

+1

@ O.O。根本不愚蠢。我試圖找到它提到這個原始文章,我不能。嘗試搜索「CoordinatorLayout示例」以獲取更多詳細信息。 –

+0

謝謝**科裏**我是新來的android,它似乎沒有很好的文檔。我想我需要去解決這個問題。再次感謝。 –

相關問題