Android LinearLayout子项未显示

Android LinearLayout子项未显示,android,Android,我想创建下面的布局 布局结构: LinearLayout --> ImageButton --> TextView --> Button 我想要的是在父布局内水平显示Textview的文本中心 但我无法到达那里,现在按钮不见了: 我的代码: <LinearLayout android:layout_width="match_parent" android:layout_height="60dp"

我想创建下面的布局

布局结构:

LinearLayout
  --> ImageButton
  --> TextView
  --> Button
我想要的是在父布局内水平显示Textview的文本中心

但我无法到达那里,现在按钮不见了:

我的代码:

     <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            android:background="@color/toolBar"
            android:gravity="center">

            <ImageButton
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="?attr/homeAsUpIndicator"
                android:background="@drawable/back_button"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                android:onClick="activityFinish"
                android:clickable="true"
                android:focusable="true"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/qaa_book_name"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:textStyle="bold"
                android:gravity="center"
                android:text="xxx"
                />

            <Button
                android:id="@+id/qaa_end_test"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:visibility="visible"
                android:text="yyy"
                android:textColor="@color/white"/>

        </LinearLayout>

检查此更新的布局

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:background="@color/toolBar"
        android:gravity="center">

        <ImageButton
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="?attr/homeAsUpIndicator"
            android:background="@drawable/back_button"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:onClick="activityFinish"
            android:clickable="true"
            android:focusable="true"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/qaa_book_name"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            android:gravity="center"
            android:text="xxx"
            android:layout_weight="1"
            />

        <Button
            android:id="@+id/qaa_end_test"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:visibility="visible"
            android:text="yyy"
            android:textColor="@color/white"/>

    </LinearLayout>


编辑:

据文献记载,

当您设置android:orientation以指定子视图 以行或列的形式显示

您可以将单个子视图上的android:layout\u weight设置为 指定“线性布局”如何在其包含的视图之间划分剩余空间 包含


android:layout\u weight:该属性根据视图在屏幕上应占据的空间大小为视图指定一个“重要性”值。较大的权重值允许其展开以填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组中的任何剩余空间都将按其声明权重的比例指定给子视图默认权重为零

这意味着,当您将权重1(在您的示例中为
TextView
)放入容器中时,它将至少占用所有空间,除非最后一个元素(
Button
在我们的示例中)获得容器中的空间,因此
TextView
展开以填充所有空间一次
Button
通过其wrap\u content属性占用空间

请参阅本指南


希望现在,我说清楚了

检查此更新的布局

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        android:background="@color/toolBar"
        android:gravity="center">

        <ImageButton
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="?attr/homeAsUpIndicator"
            android:background="@drawable/back_button"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            android:onClick="activityFinish"
            android:clickable="true"
            android:focusable="true"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/qaa_book_name"
            android:textColor="@color/white"
            android:textSize="20sp"
            android:textStyle="bold"
            android:gravity="center"
            android:text="xxx"
            android:layout_weight="1"
            />

        <Button
            android:id="@+id/qaa_end_test"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:visibility="visible"
            android:text="yyy"
            android:textColor="@color/white"/>

    </LinearLayout>


编辑:

据文献记载,

当您设置android:orientation以指定子视图 以行或列的形式显示

您可以将单个子视图上的android:layout\u weight设置为 指定“线性布局”如何在其包含的视图之间划分剩余空间 包含


android:layout\u weight:该属性根据视图在屏幕上应占据的空间大小为视图指定一个“重要性”值。较大的权重值允许其展开以填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组中的任何剩余空间都将按其声明权重的比例指定给子视图默认权重为零

这意味着,当您将权重1(在您的示例中为
TextView
)放入容器中时,它将至少占用所有空间,除非最后一个元素(
Button
在我们的示例中)获得容器中的空间,因此
TextView
展开以填充所有空间一次
Button
通过其wrap\u content属性占用空间

请参阅本指南

希望现在,我说清楚了




问题出在您的文本视图中{android:layout_width=“match_parent”}替换它
像这样的android:layout\u width=“wrap\u content”

问题出在您的文本视图中{android:layout_width=“match_parent”}替换它
像这样的android:layout\u width=“wrap\u content”

LinearLayout
为其子级支持一个名为
layout\u weight
的特殊属性。在一个或多个子视图上指定此属性将使
LinearLayout
首先按常规布局所有内容,然后将剩余的所有额外空间分配给指定了权重的子视图(并根据每个子视图的权重值将此额外空间除以)

最常见的举重案例是只给一个孩子举重,实际上是说“给所有其他孩子所需的空间,然后给这个孩子所有剩余的空间”。这正是你在这里想要的;在两个视图上,应该有两个视图:WrAPHythult,然后在中间填充一个视图,以填充剩余的空间。

当您只有一个孩子体重
layout\u
时,通常您会将其匹配尺寸(水平布局的宽度,垂直布局的高度)设置为
0dp
。这是因为LinearLayout在确定剩余的“额外”空间量之前,仍然必须进行第一次布局,并且调整0dp视图的大小比调整
wrap\u内容的大小要快。。。不管你给它什么尺寸,它都会被拉伸到相同的尺寸

考虑到所有这些,您的布局应该如下所示:

<LinearLayout
    android:orientation="horizontal"
    ...>

    <ImageButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        .../>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        .../>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        .../>

</LinearLayout>

LinearLayout
为其子级支持一个名为
layout\u weight
的特殊属性。在一个或多个子视图上指定此属性将使
LinearLayout
首先按常规布局所有内容,然后将剩余的所有额外空间分配给指定了权重的子视图(并根据每个子视图的权重值将此额外空间除以)

最常见的举重案例是只给一个孩子举重,实际上是说“给所有其他孩子所需的空间,然后给这个孩子所有剩余的空间”。这正是你在这里想要的;在两个视图上,应该有两个视图:WrAPHythult,然后在中间填充一个视图,以填充剩余的空间。

当您只有一个孩子体重
layout\u
时,通常您会将其匹配尺寸(水平布局的宽度,垂直布局的高度)设置为
0dp
。这是因为LinearLayout仍然必须在确定之前完成第一步,以布局所有内容
<LinearLayout
    android:orientation="horizontal"
    ...>

    <ImageButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        .../>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        .../>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        .../>

</LinearLayout>