Android 按钮未在LinearLayout中显示

Android 按钮未在LinearLayout中显示,android,button,android-layout,Android,Button,Android Layout,我试图在文本视图之后的线性布局中添加按钮,但它没有显示 这是我的布局代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layo

我试图在
文本视图
之后的
线性布局中添加
按钮
,但它没有显示

这是我的布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="45dip">

    <TextView android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:paddingLeft="5dip"
        android:paddingRight="5dip"
        android:textStyle="bold"
        android:textSize="17dip"
        android:gravity="center_vertical"
        android:id="@+id/tvChild"
        android:text="Children"
        android:textColor="#ffCCCC22" />

    <Button android:id="@+id/submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Submit" />
</LinearLayout>

TextView
显示正确,文本正确,但我得到的是一个三到四行的大空白,而不是
按钮


我遗漏了什么?

问题是您为
文本视图设置了
android:layout\u width=“fill\u parent”
,因此仅显示
文本视图就需要全屏宽度。它无法显示
按钮

这里有两个选项供您选择:

  • 在单行上添加
    TextView
    按钮

    TextView
    layout\u width
    属性更改为
    wrap\u content

  • 垂直添加
    文本视图
    按钮

    LinearLayout
    的方向属性更改为
    vertical

  • 试试这个

    <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout android:id="@+id/LinearLayout01"
            android:layout_width="fill_parent" android:layout_height="45dip"
            xmlns:android="http://schemas.android.com/apk/res/android">
            <TextView android:layout_width="wrap_content"
                android:layout_height="45dip" android:paddingLeft="5dip"
                android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip"
                android:gravity="center_vertical" android:id="@+id/tvChild"
                android:text="Children" android:textColor="#ffCCCC22"
                />
                <Button android:id="@+id/submit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:text="Submit" />
        </LinearLayout>
    
    
    
    使用android:layout\u width=“wrap\u content”
    而不是
    android:layout\u width=“fill\u parent”
    对于
    TextView
    按钮
    您也可以尝试类似的方法

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical"
        android:layout_margin="10dp"
    
        >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="3"
            android:layout_gravity="center"
            android:text="Hello World!"/>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:text="Submit"/>
    
    </LinearLayout>
    
    
    
    将TextView的android:layout\u width=“fill\u parent”更改为android:layout\u width=“wrap\u content”或在您的LinearLayout标签中添加android:orientation=“vertical”即使OP是新的,您也不应该要求向上投票并回答接受。阅读我写的内容:即使他是新的,您也不应该这样做。你可以告诉他如何接受答案。不要强迫用户接受你的答案,甚至不要投票。您的答案无法解释您的解决方案为何有效。我只告诉这个新用户bcz每个用户都不知道接受答案的确切含义。。我得到了它。。谢谢你宝贵的时间