2017-08-22 3 views
0

これは一般に認められている方法ですか?私のホーム画面は、以下のコードを使用して、まず下の画像のように見え、ユーザーにゲームを開始するよう促します。Android XMLレイアウトレイヤー化

Original home screen

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <com.example.android.snake.SnakeView 
    android:id="@+id/snake" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
       tileSize="24" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TextView 
     android:id="@+id/text" 
      android:text="@string/snake_layout_text_text" 
      android:visibility="visible" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center_horizontal" 
      android:textColor="#ff8888ff" 
      android:textSize="24sp"/> 
    </RelativeLayout> 
</FrameLayout> 

これはエラーなしで働いていたが、私は最後の5得点(ボタン)、ユーザの入力(のEditText)とショーのためにアイテムを追加するために必要なむしろを通じてプレーヤーを置くよりも、その下にこのプロンプトを望んでいました複数の画面。私が読んだところでは、競合のないRelativeLayoutの中にFrameLayoutを埋め込むことができるはずですが、Show Scoresボタンにしか作用せず、ゲームのプロンプトが表示されません。デザインビューでチェックすると、下半分(現在表示中)がゲームをプレイできるクラスをインスタンス化できないというエラーが表示されます。私は単にこれを修正する方法について私自身の傍にいて、将来のプロジェクトでこれが再び起こらないように、間違ったことを理解したいと思います。以下は、エラーを引き起こす変更されたコードです。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Username" 
     android:id="@+id/textViewName"/> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/editTextName"/> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     style="?android:attr/buttonStyleSmall" 
     android:text="Add" 
     android:id="@+id/btnAdd"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Last 5 Games" 
     android:id="@+id/btnLastFive"/> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <com.example.android.snake.SnakeView 
     android:id="@+id/snake" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tileSize="24" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 

     <TextView 
      android:id="@+id/text" 
      android:visibility="visible" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center_horizontal" 
      android:textColor="#ff8888ff" 
      android:textSize="24sp"/> 
    </RelativeLayout> 
    </FrameLayout> 
</LinearLayout> 

Proposed layout

+0

したがって、提案されたレイアウトとまったく同じに見えるレイアウトをしたいですか?または、あなたが望むのは、提案されたレイアウトを表示するために大きな青いラベル「Snake press up to play」をクリックしたときです。 –

+0

はい。提案されたレイアウトは、Android Studioでデザインビューが現在示しているものです。理論上のホーム画面は、下半分がオリジナルに表示されているものを表示しているように、まったく同じように表示されます。 –

+0

'com.example.android.snake.SnakeView'の内容(「現在表示されている下半分がゲームをプレイできるクラスをインスタンス化できないというエラーが表示されています」) ?もう1つの質問は、ユーザーが「再生を押してください」をクリックするとラベルが消え、「com.example.android.snake.SnakeView」だけが表示されるようにします。 –

答えて

0

あなたのルートレイアウトにカスタムビューcom.example.android.snake.SnakeViewの名前空間を追加し、カスタムビュー属性の宣言の前に名前空間接頭辞を追加する必要があります。

だから、それはまた、そうでない場合、あなたが勝った、あなたはTextViewの@+id/textから「再生するには蛇を押すまで」テキストの内容を削除しているようだtileSizeは、例えばなりapp:tileSize

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    ...... 

    <com.example.android.snake.SnakeView 
     android:id="@+id/snake" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:tileSize="24" /> 

    ..... 

</LinearLayout> 

、名前空間は「アプリ」であると仮定するとそれを見ることができない。

+0

あなたの提案を追加しましたが、画面の下半分はまだSnakeViewクラスの読み込み/インスタンス化に失敗します。また、プレスアップ用のテキストコンテンツはSnakeViewのメソッドの一部であり、ハードコーディングされたテキストではありません。これは複数のスクリーンの1つになる可能性があります。 –

+0

パッケージ名は正しいですか? –

+0

はい、偶然に何も変わっていないことを確かめるために、ASで確認しました。 –