【问题标题】:linear and relative layouts线性和相对布局
【发布时间】:2020-06-20 08:38:51
【问题描述】:
      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"
      android:orientation="vertical"  >

      <ImageView
      android:layout_width="match_parent"
      android:layout_height="200dp"
      android:src="@drawable/androidparty" />

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

       <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="DEALS IN:-"
       android:padding="10dp"/>

      </LinearLayout>
     
      </RelativeLayout>

      

在相对布局中,当我将高度和重量设置为 match_parent 时,我的文本滑到设计面板的底部并且不可见,为什么会发生这种情况?

【问题讨论】:

    标签: android xml android-layout layout


    【解决方案1】:

    您必须指定视图在RelativeLayout 中的定位方式。
    您可以查看doc:

    每个视图的位置可以指定为相对于同级元素(例如在另一个视图的左侧或下方)或相对于父RelativeLayout区域的位置(例如与底部、左侧或中心)。

    例如使用android:layout_alignParentTop="true":

    <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    
           <TextView
             android:layout_alignParentTop="true"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="DEALS IN:-"
             android:padding="10dp"/>    
    
    </RelativeLayout>
    

    在您的布局中还有另一个问题:

    <LinearLayout
       android:orientation="vertical">
    
       <RelativeLayout
          android:layout_height="match_parent"/>
    
       <TextView/>
    
    </LinearLayout>
    

    RelativeLayout 有android:layout_height="match_parent",这样它会填充父视图,而TextView 在RelativeLayout 之外。

    【讨论】:

    • 您是否建议这里的 RelativeLayout 是 LinearLayout 的子级,因此这是我没有遇到的问题?
    • @mittal_sahil RelativeLayout 是 LinearLayout 的子项,但这不是问题(这取决于您要实现的目标)。
    【解决方案2】:

    我认为这里的问题是标签错误。

    这应该会有所帮助:

    <?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:src="@drawable/androidparty" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="DEALS IN:-" />
        </RelativeLayout>
    </LinearLayout>
    

    我建议你在有更多可能性的地方使用约束布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 2023-03-09
      • 2019-12-12
      • 2017-10-01
      相关资源
      最近更新 更多