[英]TextView invisible in horizontal layout
我正在做一個大書呆子牧場的教程,此時它似乎對我不起作用。 我用我們的代碼對我的代碼進行了三次檢查,但它不起作用。 XML是一個新的content_quiz.xml文件,位於layout-land中,它可以部分工作,因為下一個按鈕顯示在正確的位置。
橫向的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);
}
}
我在我的手機和模擬器上在android 6和6.0.1上測試了這個。 兩者都給出錯誤的樣式並丟棄TextView。 這里出了什么問題?
FrameLayout允許沿Z軸放置。 它旨在阻擋屏幕上的區域以顯示單個項目。 通常,FrameLayout應該用於保存單個子視圖,因為很難以可擴展到不同屏幕大小而兒童不會相互重疊的方式組織子視圖。
對於您的問題您應該切換到相對/線性布局
我已經
Layout
使用Relative Layout
,進行一些改動。 使用Relative Layout
而不是Frame 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
。
這是Landscape Screen
。
嘗試在你的TextView中放置android:text="test"
,這樣你就可以看到它是否顯示出來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.