【问题标题】:LinearLayout child pushes other viewsLinearLayout 子级推送其他视图
【发布时间】:2018-01-08 18:53:11
【问题描述】:

我的活动的主要布局是LinearLayout。以它有两个孩子为例。一个RelativeLayout 和一个按钮。 RelativeLayout 的宽度和高度设置为 match_parent 和 Button,match_parentwrap_content。不幸的是,RelativeLayout 按下下面的按钮,直到它不可见。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

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

为什么会这样?我尝试对RelativeLayout 进行调整,将高度更改为0dp,将重量更改为1。它确实使按钮可见,但我不确定这是否可取,因为我只更改了RelativeLayout 的高度和重量.

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    试试这个

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_height="match_parent">
    
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
    
            </RelativeLayout>
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>
    

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:fillViewport="true"
        android:layout_height="match_parent">
    
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
           android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_above="@+id/btn"
            android:layout_height="match_parent">
    
            <ImageView
                android:layout_width="match_parent"
                android:src="@drawable/disha"
                android:layout_height="match_parent" />
    
        </RelativeLayout>
    
        <Button
            android:id="@+id/btn"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:text="NIlu"
            android:layout_alignParentBottom="true"
            android:layout_height="wrap_content" />
    </RelativeLayout>
    </ScrollView>
    

    【讨论】:

      【解决方案2】:

      LinearLayout 是垂直或水平的。您的第一个孩子 RelativeLayoutmatchParent 然后它将覆盖整个 Outer layout 。 解决方案可以是多种解决方案,具体取决于您要放置 Button 的确切位置。一种方法是。

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          >
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_above="@+id/btn"
              android:layout_height="match_parent">
      
              <ImageView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" />
      
          </RelativeLayout>
      
          <Button
              android:id="@+id/btn"
              android:layout_width="match_parent"
              android:layout_margin="10dp"
              android:layout_alignParentBottom="true"
              android:layout_height="wrap_content" />
      </RelativeLayout>
      

      【讨论】:

        【解决方案3】:

        改变你的代码喜欢

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        
            <RelativeLayout
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                >
        
                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
        
            </RelativeLayout>
        
            <Button
                android:text="@string/app_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
        

        为我工作.. 你应该把android:layout_weight="1" 给相对布局

        【讨论】:

        • 如果您不想更改根布局,请使用我的 ans。你也可以关注@nilu 的回答
        • 感谢好友@LearningAlways
        【解决方案4】:

        //试试这个

        <?xml version="1.0" encoding="utf-8"?>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp" />
        
        </RelativeLayout>
        
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        </LinearLayout>
        

        【讨论】:

          【解决方案5】:

          这似乎工作得很好。

          我尝试对 RelativeLayout 进行调整,将高度更改为 0dp 和权重为 1

          代码如下所示:

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="match_parent"
          android:layout_height="match_parent">
          
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              >
          
              <ImageView
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  />
          </RelativeLayout>
          
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              />
          
          </LinearLayout>
          

          【讨论】:

            【解决方案6】:

            在您的布局中,RelativeLayout 是您的LinearLayout 的第一个孩子。因为,您已将高度和宽度设置为 match_parent,它会占用所有可用空间,将按钮按出视图。

            当您将RelativeLayout 高度设置为0dp 并将权重设置为1 时,LinearLayout 首先根据指定的尺寸布置所有子项,然后根据权重分配剩余空间。在这种情况下,相对布局是0dp,因此它在第一次传递中不占用空间,并且按钮根据其onMeasure(假设100dp)占用空间。剩余空间现在分配给RelativeLayout,因为它的权重设置为 1。有关更多详细信息,请参阅 LinearLayout 文档here

            您应该考虑使用RelativeLayout/ConstraintLayout 作为您的根元素。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-11-13
              • 1970-01-01
              • 2018-08-31
              • 1970-01-01
              • 2017-11-03
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多