Android 单选按钮未显示

Android Radio Buttons Not Displaying

我正在学习 Android 开发,并且正在尝试学习如何使用单选按钮。当我 运行 下面的代码时,我看到了字符串 "Attending?" 但我没有看到任何单选按钮或其标签。我错过了什么?

MainActivity.java

package com.example.andriod.assignment1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    @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_main, 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);
    }

    public void onRadioButtonClicked(View view) {
        // Is the button now checked?
        boolean checked = ((RadioButton) view).isChecked();
        // Check which radio button was clicked
        switch(view.getId()) {
            case R.id.yes_radio:
                if (checked) {
                    //your code here
                    Toast.makeText(getApplicationContext(), "Thanks for coming!", Toast.LENGTH_LONG).show();
                    break;
                }
            case R.id.no_radio:
                if (checked) {
                    //your code here
                    Toast.makeText(getApplicationContext(), "See you next time!", Toast.LENGTH_LONG).show();
                    break;
                }
        }
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="Attending?" android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/yes_radio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="@string/yes" />
        <RadioButton
            android:id="@+id/no_radio"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onRadioButtonClicked"
            android:text="@string/no" />
    </RadioGroup>

</LinearLayout>

When I run the below code I see the string "Attending?" but I don't see any radio buttons or their labels

RadioButton's 不可见,因为所有 space 由于 android:layout_height="match_parent"TextView 填充。

将 TextView 布局height更改为android:layout_height="wrap_content"以使其正常工作。