首页 > 解决方案 > 线性和相对布局

问题描述

      <?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 时,我的文本滑到设计面板的底部并且不可见,为什么会发生这种情况?

标签: androidxmlandroid-layoutlayout

解决方案


您必须指定视图在RelativeLayout.
您可以查看文档

每个视图的位置可以指定为相对于同级元素(例如在另一个视图的左侧或下方)或相对于父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>

RelativeLayouthas并以这种android:layout_height="match_parent"方式填充父视图,TextViewRelativeLayout.

在此处输入图像描述


推荐阅读