代码之家  ›  专栏  ›  技术社区  ›  EagerToLearn

线性布局子项未显示

  •  0
  • EagerToLearn  · 技术社区  · 7 年前

    我想创建下面的布局。

    enter image description here

    LinearLayout
      --> ImageButton
      --> TextView
      --> Button
    

    我想要的是在父布局中水平显示Textview的文本中心。

    但我到不了,现在按钮不见了:

    enter image description here

    我的代码:

         <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>
    
    5 回复  |  直到 7 年前
        1
  •  1
  •   Jeel Vankhede    7 年前

    看看这个 更新的布局 :

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

    编辑:

    根据 LinearLayout 文档,

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

    你可以设置 android:layout_weight 论个体儿童观 指定线性布局如何将剩余空间在其所属视图之间划分 包含。


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

    TextView 在你的情况下) ,它将至少占用所有空间,除非紧靠最后一个元素 ( Button 在我们的情况下) 在容器中获得空间,所以 文本框 按钮 包装内容

    请参阅本指南 official doc

    希望现在,我说清楚了!

        2
  •  1
  •   Ben P.    7 年前

    LinearLayout 为其子级支持一个名为 layout_weight . 在一个或多个子视图上指定此属性将使 线性布局 首先将所有内容按正常方式进行布局,然后将剩余的所有额外空间分配给指定了权重的子级(并根据每个子级的值将该额外空间除以权重)。

    wrap_content ,然后在中间的一个视图中填充剩余的空间。

    布局重量 ,通常您将其匹配尺寸(水平布局的宽度,垂直布局的高度)指定为 0dp . 这是因为LinearLayout在确定剩余的“额外”空间量之前,仍然需要先进行第一次布局,调整0dp视图的大小要比调整视图的大小快 查看。。。不管你给它什么尺寸,它都会被拉伸到相同的尺寸。

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

    <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>
    
        3
  •  0
  •   Rajan Aghara    7 年前
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_image">
    
        <ImageView
            android:id="@+id/img_back"
            android:layout_width="@dimen/_30sdp"
            android:layout_height="@dimen/_30sdp"
            android:src="@drawable/ic_arrow_back_black_24dp"
            android:onClick="onClick"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:text="xxx"
            android:textColor="@android:color/black"
            android:textSize="@dimen/_24sdp"
            android:textStyle="bold" />
    
        <Button
            android:id="@+id/btn_yyy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="yyy"
            android:layout_gravity="end"
            android:textAllCaps="false"
            android:textSize="@dimen/textsize_18sp" />
    
    </FrameLayout>
    
        4
  •  0
  •   Rocky    7 年前
       <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"
            />
    
       Problem is in your text view {android:layout_width="match_parent"}  replace it 
       like this   android:layout_width="wrap_content"
    
        5
  •  0
  •   Whole Brain    7 年前

    LinearLayout中所有视图的默认权重为0。视图的权重越小,优先级越高。

    android:layout_weight="1" 当其他视图处于默认权重时,则具有默认权重的视图将获得优先级。它们在线性布局中的大小将在权重为1的视图之前进行属性化。