【问题标题】:TextView invisible in horizontal layoutTextView 在水平布局中不可见
【发布时间】:2016-05-24 09:39:01
【问题描述】:

我正在做一个关于大书呆子牧场的教程,目前它似乎对我不起作用。我用他们的三次检查了我的代码,但它不起作用。 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。这里出了什么问题?

【问题讨论】:

  • 共享代码,您将值设置为 textView (R.id.question_text_view)
  • 对于测试用例删除android:padding="24dp"
  • @IntelliJAmiya 使用 android:configChanges="orientation" 是不好的方法。阅读更多link
  • 我建议你使用RelativeLayout作为父包装,FrameLayout应该在你想要元素覆盖时使用

标签: java android xml textview


【解决方案1】:

FrameLayout 允许沿 Z 轴放置。它旨在阻止屏幕上的一个区域以显示单个项目。通常,应该使用 FrameLayout 来保存单个子视图,因为很难以一种可缩放到不同屏幕尺寸的方式组织子视图,而不会使子视图相互重叠。

对于您的问题,您应该切换到相对/线性布局

【讨论】:

  • 谢谢。但是,如何获得线性布局右下角的“下一步”按钮?
  • @DanielsWrath 您可以在Linear Layout 中使用android:gravity="bottom|right"
  • @jaydroider 我确实可以做到这一点,但我的布局的另一部分需要居中。所以我不想把所有东西都推到右下角。
  • @DanielsWrath 检查我下面的答案,它会对你有所帮助。
【解决方案2】:

我使用Relative Layout 进行了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

【讨论】:

  • @DanielsWrath 很高兴它成功了干杯! up-vote 也谢谢 :)。
  • 如果我得到 15 个代表我会的 ;)
【解决方案3】:

尝试将android:text="test" 放入您的TextView,这样您就可以查看它是否显示。

【讨论】:

  • 我没有足够的声望来添加评论
  • 我在 TextView 中添加了android:text="Test",但它没有显示出来
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-16
  • 1970-01-01
相关资源
最近更新 更多