【问题标题】:Second addView call to LinearLayout does not display对 LinearLayout 的第二个 addView 调用不显示
【发布时间】:2016-05-03 20:48:18
【问题描述】:

我正在尝试将 2 行添加到我的线性布局(称为容器)中。单击主题按钮(mathButton 或 bioButton)时会调用这两行。

我要添加的第一行(带有字符串“Set Difficulty:”的 TextView)。接下来是一行三个切换按钮,允许用户选择难度(“简单”、“中等”、“困难”)。

当我选择一个主题时,TextView 对象的难度标题会以动画形式显示。但是,按钮不是。我不确定为什么会这样。这里的任何帮助都会很棒!

活动代码:

public class LaunchScreen extends Activity {

private TextView gameTitle;

private Button startGame;

private ToggleButton mathButton;
private ToggleButton bioButton;

private CharSequence difficultyLevel;
private CharSequence topic;
private Boolean lessonModeOn;

private Boolean difficultyAppeared;
private Boolean lessonModeAppeared;

private TextView difficultyTitle;

private ViewGroup mContainerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_launch_screen);

    mContainerView = (ViewGroup) findViewById(R.id.gameSettingsContainer);
    //booleans to keep track of added difficulty and lesson mode
    difficultyAppeared = false;
    lessonModeAppeared = false;

    gameTitle = (TextView) findViewById(R.id.gameTitle);
    gameTitle.setText("BrainTeaz!");

    startGame = (Button) findViewById(R.id.startGame);
    startGame.setText("Start");

    Toast.makeText(LaunchScreen.this, "Welcome! Select a topic.", Toast.LENGTH_SHORT).show();
    topic = null;
    mathButton = (ToggleButton) findViewById(R.id.mathButton);
    mathButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                topic = mathButton.getText();
                bioButton.setChecked(false);
            }
            else {
                if(!bioButton.isChecked()) {
                    topic = null;    
                }

            }
            if (!difficultyAppeared) {
                animateDifficulty();
            }
        }
    });
    bioButton = (ToggleButton) findViewById(R.id.bioButton);
    bioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                topic = bioButton.getText();
                mathButton.setChecked(false);
            }
            else {
                if(!mathButton.isChecked())
                {
                    topic = null;
                }

            }
            if (!difficultyAppeared) {
                animateDifficulty();
            }
        }
    });
}
public void animateDifficulty() {
    Toast.makeText(LaunchScreen.this, "Choose a difficulty.", Toast.LENGTH_SHORT).show();
    ViewGroup newView1 = (ViewGroup) LayoutInflater.from(this).inflate(
            R.layout.launch_screen_difficulty_settings_title, mContainerView, false);

    ViewGroup newView2 = (ViewGroup) LayoutInflater.from(this).inflate(
            R.layout.launch_screen_difficulty_settings, mContainerView, false);

    difficultyLevel = null;

    final ToggleButton easyDifficulty = (ToggleButton) newView2.findViewById(R.id.easyDifficulty);
    final ToggleButton medDifficulty = (ToggleButton) newView2.findViewById(R.id.medDifficulty);
    final ToggleButton hardDifficulty = (ToggleButton) newView2.findViewById(R.id.hardDifficulty);

    easyDifficulty.setChecked(false);
    medDifficulty.setChecked(false);
    hardDifficulty.setChecked(false);

    easyDifficulty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
                difficultyLevel = easyDifficulty.getText();
                //guarantee no other button is checked
                medDifficulty.setChecked(false);
                hardDifficulty.setChecked(false);
            } else {
                difficultyLevel = null;
                // The toggle is disabled
                // The toggle is disabled, enable easy as default
                if (!hardDifficulty.isChecked() && !medDifficulty.isChecked()) {
                    difficultyLevel = null;   
                }
            }
            if (!lessonModeAppeared) {
                animateLessonMode();
            }
        }
    });
    medDifficulty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
                difficultyLevel = medDifficulty.getText();
                //guarantee no other button is checked
                easyDifficulty.setChecked(false);
                hardDifficulty.setChecked(false);
            } else {
                difficultyLevel = null;
                // The toggle is disabled, enable easy as default
                //if both aren't checked then
                if (!easyDifficulty.isChecked() && !hardDifficulty.isChecked()) {
                    difficultyLevel = null;
                }

            }
            if (!lessonModeAppeared) {
                animateLessonMode();
            }
        }
    });
    hardDifficulty.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
                difficultyLevel = hardDifficulty.getText();
                Toast.makeText(getApplicationContext(), "Hard difficulty selected.", Toast.LENGTH_SHORT).show();
                //guarantee no other button is checked
                easyDifficulty.setChecked(false);
                medDifficulty.setChecked(false);
            } else {
                if (!easyDifficulty.isChecked() && !medDifficulty.isChecked()) {
                    difficultyLevel = null;
                }
            }
            if (!lessonModeAppeared) {
                animateLessonMode();
            }
        }
    });
    mContainerView.addView(newView1, 4);
    Log.d("vikram", "you should see difficulty title");
    mContainerView.addView(newView2, 5);
    Log.d("vikram","you should see buttons");
    difficultyAppeared = true;
}

LaunchScreen 布局:

    <?xml version="1.0" encoding="utf-8"?>
<!--
Game settings will be programmatically added to this parent Linear Layout
-->
<LinearLayout
    android:id="@+id/gameSettingsContainer"
    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="com.example.vb1115.multchoicequestion.LaunchScreen"
    android:orientation="vertical"
    android:animateLayoutChanges="true">

    <TextView android:id="@+id/gameTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Game Title"
        android:textSize="40dp"
        android:layout_gravity="center_horizontal"/>
    <Button android:id="@+id/startGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:layout_gravity="center_horizontal"
        android:onClick="startGame"/>
    <!--
    Set Topic - Once this is clicked, new 2 rows of game settings
    will be animated
    -->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Topic:"
        android:textSize="20dp"
        android:layout_gravity="center_horizontal"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal">
        <ToggleButton android:id="@+id/mathButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="Math"
            android:textOn="Math"
            android:text="Math"/>
        <ToggleButton android:id="@+id/bioButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="Biology"
            android:textOn="Biology"
            android:text="Biology"/>
    </LinearLayout>
</LinearLayout>

第一行 (newView1) 的布局 (launch_screen_difficulty_settings_title.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">
    <TextView
        android:id="@+id/difficultyTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set Difficulty:"
        android:textSize="20dp"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

第二行(newView2)的布局(launch_screen_difficulty_settings):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">
        <ToggleButton android:id="@+id/easyDifficulty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="Easy"
            android:textOn="Easy"
            android:text="Easy"/>
        <ToggleButton android:id="@+id/medDifficulty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="Med"
            android:textOn="Med"
            android:text="Med"/>
        <ToggleButton android:id="@+id/hardDifficulty"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff="Hard"
            android:textOn="Hard"
            android:text="Hard"/>
</LinearLayout>

【问题讨论】:

  • 在第一行和第二行的布局中,您的高度匹配父项,因此它占用了容器布局中的所有可用空间。尝试将行的高度更改为 wrap_content 或将高度更改为 0dp 并添加值为 1 的权重参数
  • @Vik 你是否为你的 LinearLayout 调用了无效?
  • @Beyka 是的,对于那些行,我将高度更改为 wrap_content 并且它起作用了!感谢您的快速修复。

标签: android android-layout android-studio android-linearlayout


【解决方案1】:

这是我为解决问题所做的更改(针对两行):

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

再次感谢Beyka

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 2018-12-05
    相关资源
    最近更新 更多