【问题标题】:LinearLayout only show first itemLinearLayout 只显示第一项
【发布时间】:2019-07-03 12:07:12
【问题描述】:

LinearLayout 有问题,它只显示第一个孩子。我找到了solution,但我没有工作。
这是我的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.hoangdang.diemdanh.QRCode.QRCodeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/qrcode_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary_darker"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="horizontal">

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/qrCode_imageView"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/generate_code_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/generate_code"
            android:layout_marginRight="@dimen/de_btn_padding"
            android:layout_marginLeft="@dimen/de_btn_padding"/>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

这是我改变ButtonImageView的位置时的结果

【问题讨论】:

  • 水平LinearLayout 内的Views 都有layout_widths 的match_parent。第二个被推到一边。
  • 感谢您的回复,我的英语不好我被horizontalvertical误解了

标签: android android-linearlayout


【解决方案1】:

我认为您必须将方向设置为垂直于线性布局,如下所示

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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.hoangdang.diemdanh.QRCode.QRCodeActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/qrcode_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary_darker"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="vertical">

        <android.support.v7.widget.AppCompatImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/qrCode_imageView"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>

        <android.support.v7.widget.AppCompatButton
            android:id="@+id/generate_code_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/generate_code"
            android:layout_marginRight="@dimen/de_btn_padding"
            android:layout_marginLeft="@dimen/de_btn_padding"/>

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

【讨论】:

    【解决方案2】:

    您已将 ImageView 的宽度定义为 MATCH_PARENT,因此它将占用整个父级,而父级为 HORIZONTAL,则第二个视图将永远不会显示。这可能会帮助你

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/qrcode_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary_darker"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>
    
    </android.support.design.widget.AppBarLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="horizontal">
    
        <android.support.v7.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/qrCode_imageView"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>
    
        <android.support.v7.widget.AppCompatButton
            android:id="@+id/generate_code_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/generate_code"
            android:layout_marginRight="@dimen/de_btn_padding"
            android:layout_marginLeft="@dimen/de_btn_padding"/>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      你可以在图片和按钮中使用 weight 属性来试试这个

      <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      android:orientation="horizontal">
      
      <android.support.v7.widget.AppCompatImageView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/qrCode_imageView"
          android:layout_weight="1"
          android:layout_centerVertical="true"
          android:layout_centerHorizontal="true"/>
      
      <android.support.v7.widget.AppCompatButton
          android:id="@+id/generate_code_button"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="@string/generate_code"
          android:layout_marginRight="@dimen/de_btn_padding"
          android:layout_marginLeft="@dimen/de_btn_padding"/>
      

      【讨论】:

        猜你喜欢
        • 2018-12-05
        • 2016-09-13
        • 1970-01-01
        • 2021-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-23
        相关资源
        最近更新 更多