2017-06-08 144 views
-2

我遇到了问题LinearLayout,它只显示第一个孩子。我发现一个solution,但我不工作。
这里我的xml:LinearLayout只显示第一项

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

这里是结果当我改变的Button位置和ImageView

enter image description here

enter image description here

+2

水平LinearLayout里的'View's''layout_width's'match_parent'。第二个是被推到一边。 –

+1

感谢您的回应,我不擅长英语,我对'水平线'和'垂直线'有误解 –

回答

2

我认为你必须设置方向垂直于线性布局如上

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

您已经定义的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> 

1

你可以尝试在图片的使用权重属性和按钮

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