Я пытаюсь анимировать добавление 2 строк к моей линейной компоновке (называемой контейнером). Эти 2 строки вызывается, когда нажата кнопка темы (либо mathButton, либо bioButton).Второй вызов addView для LinearLayout не отображается
Первая строка, которую я хочу добавить (TextView со строкой «Set Difficulty:»). Следующий - это строка из трех ToggleButtons, позволяющая пользователю выбрать сложность («Легкий», «Средний», «Жесткий»).
Когда я выбираю тему, заголовок объекта 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>
Layout (launch_screen_difficulty_settings_title.xml) 1-й ряд (newView1):
<?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>
Layout (launch_screen_difficulty_settings) 2-й строки (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>
В ваших макетах для первой и второй строки у вас есть высота, соответствующая match_parent, поэтому она занимает все пространство в макете вашего контейнера. Попробуйте изменить высоту для строк на wrap_content orr, измените высоту на 0dp и добавьте параметр веса со значением = 1 – Beyka
@Vik вы назвали invalidate для вашего ** LinearLayout **? – Amir
@Beyka yep, для этих строк я изменил высоту на wrap_content, и это сработало! Благодарю за быстрое исправление. – Vik