0

我正在嘗試爲我的線性佈局(稱爲容器)添加兩行動畫。當單擊主題按鈕(mathButton或bioButton)時,會調用這兩行。第二次添加到LinearLayout的調用不會顯示

我想添加的第一行(帶有字符串「Set Difficulty:」的TextView)。接下來是一排三個ToggleButtons,允許用戶選擇難度(「簡單」,「中等」,「硬」)。

當我選擇一個主題時,TextView對象的難度標題以動畫顯示。但是,按鈕不是。我不知道爲什麼會發生這種情況。這裏的任何幫助都會很棒!第一行的

<?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> 

佈局(launch_screen_difficulty_settings_title.xml)(newView1):

代碼活性:

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"?> 
<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> 

佈局(launch_screen_difficulty_settings)第二排(newView2):

<?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> 
+0

在第一行和第二行的佈局中,您的高度爲match_parent,因此它將佔用容器佈局中的所有可用空間。嘗試改變行的高度wrap_content或更改高度爲0dp,並添加重量參數值= 1 – Beyka

+0

@Vik你叫** invalidate爲你的** LinearLayout **? – Amir

+0

@Beyka是的,對於那些行我改變了高度wrap_content和它的工作!感謝您的快速解決。 – Vik

回答

0

這是我改變來解決該問題(兩行):

<?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

相關問題