2016-05-24 14 views
1

大きなナンデの牧場のチュートリアルを行っていますが、この時点で私にとってはうまくいかないようです。私は自分のコードを3倍にチェックしましたが、動作しません。 XMLはlayout-landにある新しいcontent_quiz.xmlファイルで、次のボタンが適切な場所に表示されているため部分的に機能します。TextViewは水平レイアウトでは表示されません

目標はこれです: the big nerd ranch app

が、私のアプリは、次のようになります。縦にMy app

それはしかし作業を行います:水平ためvertical my app

XMLは以下の通りである。

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

    <TextView 
     android:id="@+id/question_text_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:padding="24dp" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/true_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/true_button"/> 

     <Button 
      android:id="@+id/false_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/false_button"/> 

    </LinearLayout> 

    <Button 
     android:id="@+id/next_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/next_button" 
     android:drawableRight="@drawable/arrow_right" 
     android:layout_gravity="bottom|right" 
     android:drawablePadding="4dp"/> 

</FrameLayout>  

javaアクティビティファイルは、次のとおりです。

package com.bignerdranch.android.geoquiz; 

import android.os.Bundle; 
import android.os.StrictMode; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class QuizActivity extends AppCompatActivity { 

    private static final String TAG = "QuizActivity"; 

    private Button mTrueButton; 
    private Button mFalseButton; 
    private Button mNextButton; 
    private TextView mQuestionTextView; 

    private Question[] mQuestionBank = new Question[] { 
      new Question(R.string.question_africa, false), 
      new Question(R.string.question_americas, true), 
      new Question(R.string.question_asia, true), 
      new Question(R.string.question_middle_east, false), 
      new Question(R.string.question_oceans, true), 
    }; 

    private int mCurrentIndex = 0; 

    private void updateQuestion() { 
     int question = mQuestionBank[mCurrentIndex].getTextResId(); 
     mQuestionTextView.setText(question); 
    } 

    private void checkAnswer(boolean userPressedTrue) { 
     boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue(); 
     int messageResId = 0; 

     if (userPressedTrue == answerIsTrue) { 
      messageResId = R.string.correct_toast; 
     } else { 
      messageResId = R.string.incorrect_toast; 
     } 

     Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Log.d(TAG, "onCreate(Bundle) called"); 
     setContentView(R.layout.activity_quiz); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     mQuestionTextView = (TextView) findViewById(R.id.question_text_view); 

     mTrueButton = (Button) findViewById(R.id.true_button); 
     mTrueButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       checkAnswer(true); 
      } 
     }); 

     mFalseButton = (Button) findViewById(R.id.false_button); 
     mFalseButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       checkAnswer(false); 
       //Toast.makeText(QuizActivity.this, R.string.correct_toast,  Toast.LENGTH_SHORT).show(); 
       // This is the OnClickListener, thus add QuizActivity 
      } 
     }); 

     mNextButton = (Button) findViewById(R.id.next_button); 
     mNextButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length; 
       updateQuestion(); 
      } 
     }); 

     updateQuestion(); 


    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     Log.d(TAG, "onStart(Bundle) called"); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     Log.d(TAG, "onPause(Bundle) called"); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d(TAG, "onResume(Bundle) called"); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     Log.d(TAG, "onStop(Bundle) called"); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     Log.d(TAG, "onDestroy(Bundle) called"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_quiz, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

私はこれをアンドロイド6と6.0.1で、携帯電話とエミュレータの両方でテストしました。どちらも間違ったスタイルを与え、TextViewを破棄します。ここで何がうまくいかないのですか?

+0

あなたがのTextViewに値を設定しhttp://stackoverflow.com/questions/5407752/android-layout-folders-layout-layout-port-layout-land –

+1

株式コード(R.id.question_text_view ) – Alexander

+0

テストケースを削除します。アンドロイド:padding = "24dp" ' –

答えて

1

を切り替える必要があり、私はRelative Layoutを使用してLayoutをしたいくつかの変更を適用します。 Frame Layoutの代わりにRelative Layoutを使用してください。

デフォルトでは、/res/layoutのレイアウトはポートレートとランドスケープの両方に適用されます。

あなたは一例

/res/layout/main.xmlを持っている場合。

新しいフォルダ/res/layout-landを追加し、main.xmlをコピーして必要な調整を加えることができます。

これを参照してください。ここで

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

    <TextView 
     android:id="@+id/question_text_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerInParent="true" 
     android:text="The pacific ocean is larger than specific ocean." 
     android:textColor="#000" /> 

    <LinearLayout 
     android:id="@+id/LL1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/question_text_view" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:layout_marginTop="5dp" 
     android:orientation="horizontal"> 

     <Button 
      android:id="@+id/true_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="True" /> 

     <Button 
      android:id="@+id/false_button" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="False" /> 

    </LinearLayout> 

    <Button 
     android:id="@+id/next_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:drawablePadding="4dp" 
     android:drawableRight="@mipmap/ic_launcher" 
     android:text="Next Button" /> 

</RelativeLayout> 

Portrait Screenです。ここで

enter image description here

Landscape Screenです。

enter image description here

+1

本当にありがとう、これは実際に動作します! – DanielsWrath

+0

@DanielsWrath喜んでそれは乾杯しました! **アップ票**それはまた感謝:)。 –

+1

私は15人の担当者を得るでしょう;) – DanielsWrath

0

android:text="test"をTextViewに入れて、表示されているかどうかを確認してください。

+0

コメントを追加する評判が足りません –

+0

TextViewに 'android:text =" Test "を追加しましたが、表示されませんでした – DanielsWrath

2

FrameLayoutでは、Z軸に沿ったプレースメントが可能です。 1つの項目を表示するために画面上の領域をブロックするように設計されています。一般的に、FrameLayoutは単一の子ビューを保持するために使用する必要があります。子供が重ならないように異なるスクリーンサイズにスケーラブルな方法で子ビューを整理するのは困難な場合があるからです。あなたの問題のために

あなたは相対/リニアレイアウト

+1

ありがとうございました。しかし、どのようにして線形レイアウトの右下に「次へ」ボタンが表示されますか? – DanielsWrath

+1

@DanielsWrathこれは 'Linear Layout'で' android:gravity = "bottom | right" 'を使って行うことができます。 –

+0

@jaydroider私はこれを本当に行うことができますが、私のレイアウトの他の部分は中心に置く必要があります。だから私はすべてを右下に押し込むつもりはない。 – DanielsWrath

関連する問題