【问题标题】:Overlapping elements in linear layout线性布局中的重叠元素
【发布时间】:2015-01-13 11:32:23
【问题描述】:

在这里,我试图并排显示假期和请假请求图像,垂直于员工信息和请假信息图像下方。但不知何故,假期和请假请求图像没有显示,似乎已经重叠。我该如何纠正这个问题?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/homescreen_bg"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:gravity="top"
        android:contentDescription="@string/Homescreen_header"
        android:src="@drawable/logoheader" />

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView 
            android:id="@+id/empinfo_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:contentDescription="@string/Homescreen_emp_info"
            android:src="@drawable/employee_info"/>

        <ImageView 
            android:id="@+id/leaveinfo_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:gravity="left"
            android:contentDescription="@string/Homescreen_leave_info"
            android:src="@drawable/leave_info"/>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <ImageView 
            android:id="@+id/holidays_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="5dp"
            android:contentDescription="@string/Homescreen_holidays"
            android:src="@drawable/holidays"/>

        <ImageView 
            android:id="@+id/leavereq_logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="10dp"
            android:gravity="left"
            android:contentDescription="@string/Homescreen_leave_req"
            android:src="@drawable/leave_request"/>

    </LinearLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-linearlayout android-imageview


    【解决方案1】:

    您需要将图像的 LinearLayout 高度更改为 wrap_content

    试试这个:

    <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:gravity="top"
            android:contentDescription="@string/Homescreen_header"
            android:src="@drawable/logoheader" />
    
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:orientation="horizontal"> <!-- Changes here -->
    
            <ImageView 
                android:id="@+id/empinfo_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:contentDescription="@string/Homescreen_emp_info"
                android:src="@drawable/employee_info"/>
    
            <ImageView 
                android:id="@+id/leaveinfo_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:gravity="left"
                android:contentDescription="@string/Homescreen_leave_info"
                android:src="@drawable/leave_info"/>
    
        </LinearLayout>
    
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:orientation="horizontal"> <!-- Changes here -->
    
            <ImageView 
                android:id="@+id/holidays_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:contentDescription="@string/Homescreen_holidays"
                android:src="@drawable/holidays"/>
    
            <ImageView 
                android:id="@+id/leavereq_logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="10dp"
                android:gravity="left"
                android:contentDescription="@string/Homescreen_leave_req"
                android:src="@drawable/leave_request"/>
    
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      尝试将layout_weight 放在水平线性布局内的图像视图上,并将线性布局的layout_height 设置为wrap_content。不要忘记将layout_width 更改为0dp。并将整个布局放入ScrollView

      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
          <LinearLayout
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
              <ImageView
                  android:id="@+id/header"
                  android:layout_width="match_parent"
                  android:layout_height="100dp"
                  android:layout_marginLeft="10dp"
                  android:layout_marginRight="10dp"
                  android:gravity="top"
                  android:contentDescription="@string/Homescreen_header"
                  android:src="@drawable/logoheader" />
      
              <LinearLayout 
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
      
                  <ImageView 
                      android:id="@+id/empinfo_logo"
                      android:layout_weight="1"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="5dp"
                      android:contentDescription="@string/Homescreen_emp_info"
                      android:src="@drawable/employee_info"/>
      
                  <ImageView 
                      android:id="@+id/leaveinfo_logo"
                      android:layout_weight="1"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="5dp"
                      android:layout_marginRight="10dp"
                      android:gravity="left"
                      android:contentDescription="@string/Homescreen_leave_info"
                      android:src="@drawable/leave_info"/>
      
              </LinearLayout>
      
              <LinearLayout 
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
      
                  <ImageView 
                      android:id="@+id/holidays_logo"
                      android:layout_weight="1"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="10dp"
                      android:layout_marginRight="5dp"
                      android:contentDescription="@string/Homescreen_holidays"
                      android:src="@drawable/holidays"/>
      
                  <ImageView 
                      android:id="@+id/leavereq_logo"
                      android:layout_weight="1"
                      android:layout_width="0dp"
                      android:layout_height="wrap_content"
                      android:layout_marginLeft="5dp"
                      android:layout_marginRight="10dp"
                      android:gravity="left"
                      android:contentDescription="@string/Homescreen_leave_req"
                      android:src="@drawable/leave_request"/>
      
              </LinearLayout>
          </LinearLayout>
      </ScrollView>
      

      【讨论】:

        【解决方案3】:

        将员工信息和假期这两个LinearLayout包装在一个容器布局中,并将它们各自的宽度设置为android:layout_width="0dp",将权重设置为android:layout_weight="0.5",所以结果会是:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/homescreen_bg"
            android:orientation="vertical" >
        
            <ImageView
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:contentDescription="@string/Homescreen_header"
                android:gravity="top"
                android:src="@drawable/logoheader" />
        
            <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >
        
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:orientation="horizontal" >
        
                    <ImageView
                        android:id="@+id/empinfo_logo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:contentDescription="@string/Homescreen_emp_info"
                        android:src="@drawable/employee_info" />
        
                    <ImageView
                        android:id="@+id/leaveinfo_logo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="10dp"
                        android:contentDescription="@string/Homescreen_leave_info"
                        android:gravity="left"
                        android:src="@drawable/leave_info" />
                </LinearLayout>
        
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.5"
                    android:orientation="horizontal" >
        
                    <ImageView
                        android:id="@+id/holidays_logo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="5dp"
                        android:contentDescription="@string/Homescreen_holidays"
                        android:src="@drawable/holidays" />
        
                    <ImageView
                        android:id="@+id/leavereq_logo"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="10dp"
                        android:contentDescription="@string/Homescreen_leave_req"
                        android:gravity="left"
                        android:src="@drawable/leave_request" />
                </LinearLayout>
            </LinearLayout>
        
        </LinearLayout>
        

        ps如果您希望员工信息和假期徽标在彼此下方而不是并排,只需更改@987654324 @布局方向为vertical

        【讨论】:

          猜你喜欢
          • 2022-08-18
          • 1970-01-01
          • 2021-09-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-09
          相关资源
          最近更新 更多