2015-11-11 10 views
24

私はAndroidの新機能で、これをAndroid Developers - Google Groupsに投稿しようとしましたが、初心者はStack Overflowへ投稿する必要があると言えます。だから私はここにいる。CoordinatorLayoutのAppBarLayoutの下にコンテンツを置く

私は昨日のAndroid Studioの1.4.1の最新バージョンをダウンロードして、私はBuilding Your First App上の指示に従いました。私はすべてStarting Another Activityまでやりました。手順に従うとコードに表示されますが、CoordinatorLayoutAppBarLayoutを参照しないため、これらの命令はSDKの以前のバージョンのものです。明らかに、私はこのアプリを動作させるためにコードを少し変更しましたが、完全には変更しませんでした。

私の問題:あなたがStarting Another Activityの下部にある画像を見ればあなたがそれらの両方がタイトル私の最初のアプリを持っていることがわかります。コードを修正したとき、私は両方の画像/スクリーンでこのタイトルを得ることができませんでした。 (私はAppBarLayoutCoordinatorLayoutのより新しいバージョンを使用することを言及する必要があります)

Building a Simple User Interfacecontent_my.xmlの下部に述べたように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> 

である、のは、最初の画面に焦点を当ててみよう

<?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> 

がとにかくあり、私はactivity_my.xmlAppBarLayoutを追加することができますように見えます。これで問題がcontent_my.xmlの内容がAppBarLayoutToolbarの後ろではなく、それを下回ることである

<?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> 

:私のようなものを試してみました。任意のアイデアはどのようにこの問題を解決するには?

+0

申し訳ありませんが、あなたはtldrを作成できますか? – deadfish

+1

これは、ツールバーのアプリケーションタイトルを取得するために、まず最初に 'setSupportActionBar(toolbar)'を呼び出すように2つの質問があるようです。ここで 'toolbar'はツールバービューへの参照です。また、「CoordinatorLayout」と「AppBarLayout」が参照されていない理由は、Android Studioの以前のバージョンでは、「空のアクティビティ」によって完全に空白のアクティビティが作成されたためです。これらの2つの要素を持つ単純なレイアウトが作成されます。新しいオプションは「空のアクティビティ」と呼ばれます。 – 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

完璧!どうもありがとうございました。私はこれが馬鹿に聞こえるかもしれないことは知っていますが、私はgoogle 'app:layout_behavior =" @ string/appbar_scrolling_view_behavior "'を試してみました。私は誰かがこれが文書化されているURLを投稿することに感謝します。 –

+1

@ O.O。まったく愚かではない。私はこれを言及した元の記事を見つけようとしましたが、できません。詳細については、 "CoordinatorLayout example"を検索してみてください。 –

+0

**ありがとうございました** Cory **私はアンドロイドには新しく、十分に文書化されていないようです。私はこれをぶち壊す必要があると思う。再度、感謝します。 –

関連する問題