首页 > 其他分享 >Android Studio LinearLayout & RelativeLayout

Android Studio LinearLayout & RelativeLayout

时间:2023-02-26 10:22:06浏览次数:41  
标签:RelativeLayout 布局 线性 Studio 设置 方向 Android LinearLayout

1、LinearLayout

线性布局

主要分为两部分内容

第一部分是进行线性布局的方向设置 即水平方向和竖直方向

用orientation属性值进行设置

当它为horizontal的时候表示水平方向从左往右排列

当它为vertical的时候表示垂直方向从上往下排列

不指定的情况下默认是水平方向

第二部分是在线性布局的内部不同的TextView之间进行设置

水平方向的情况下 当width设置为0dp

可以用weight(权重)属性设置同一布局下不同控件的占比

比如下方的代码

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

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="横排第一个"
            android:textSize="17dp"
            android:textColor="@color/black"/>
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="横排第二个"
            android:textSize="17dp"
            android:textColor="@color/black"/>
    </LinearLayout>

在width都为0的情况下,weight分别设置为了1和2,也就是说在这个布局中两个TextView分别占1份和2份,也就是1/3和2/3

同理,vertal的情况也类似

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="竖排第一个"
            android:textSize="17dp"
            android:textColor="@color/black"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:text="竖排第二个"
            android:textSize="17dp"
            android:textColor="@color/black"/>

</LinearLayout>

运行结果如下图

 

 

 

2、RelativeLayout

RelativeLayout是相对布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp">

    <TextView
        android:id="@+id/tv_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@color/white"
        android:text="我在中间"
        android:textColor="@color/black"
        android:textSize="11sp" />


    <TextView
        android:id="@+id/tv_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@color/white"
        android:text="我在水平中间"
        android:textSize="11sp"
        android:textColor="@color/black"/>


    <TextView
        android:id="@+id/tv_center_vertal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:background="@color/white"
        android:text="我在垂直中间"
        android:textSize="11sp"
        android:textColor="@color/black"/>


    <TextView
        android:id="@+id/tv_parent_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@color/white"
        android:text="我和上级左边对齐"
        android:textSize="11sp"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/tv_parent_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@color/white"
        android:text="我和上级右边对齐"
        android:textSize="11sp"
        android:textColor="@color/black"/>


    <TextView
        android:id="@+id/tv_parent_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:text="我和上级底部对齐"
        android:textSize="11sp"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/tv_parent_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/white"
        android:text="我和上级顶部对齐"
        android:textSize="11sp"
        android:textColor="@color/black"/>


    <TextView
        android:id="@+id/tv_left_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/tv_center"
        android:layout_alignTop="@+id/tv_center"
        android:background="@color/white"
        android:text="我和tv_center左边对齐"
        android:textSize="11sp"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/tv_right_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_center"
        android:layout_alignBottom="@+id/tv_center"
        android:background="@color/white"
        android:text="我和tv_center右边对齐"
        android:textSize="11sp"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/tv_right_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/tv_center"
        android:layout_alignLeft="@+id/tv_center"
        android:background="@color/white"
        android:text="我和中间上面左对齐"
        android:textSize="11sp"
        android:textColor="@color/black"/>

    <TextView
        android:id="@+id/tv_right_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_center"
        android:layout_alignRight="@+id/tv_center"
        android:background="@color/white"
        android:text="我和中间下面右对齐"
        android:textSize="11sp"
        android:textColor="@color/black"/>

</RelativeLayout>

 

 

 

标签:RelativeLayout,布局,线性,Studio,设置,方向,Android,LinearLayout
From: https://www.cnblogs.com/Arkiya/p/17156204.html

相关文章

  • springboot3.0整合GraalVM-Native-Support,打包本地exe(native-image)。添加native-maven
    0.【idea新建一个springbootdemo项目】勾选GraalVMNativeSupport。其它略(太基础了)1.【环境准备】安装GraalVM、VisualStudio、NativeImage​​https://gitee.com/lishu......
  • Android Studio 视图对齐方式
    这里有两种类型的对齐1、layout_gravity用于当前视图相对于上级视图的对齐方式2、gravity用于下级视图相对于当前视图的对齐方式具体如下代码和图<?xmlversion="1.......
  • Audroid studio_EditText
    今天再次学了一遍button事件,发现可能由于版本问题,视频教程与实际操作有出入,暂时不做对button的总结. EditText:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutx......
  • 解决在Android studio的Button控件下background背景设置不起作用的问题
    Button控件默认的背景是深紫色的,有时候会看不清按钮上的文本,显得很不方便,想要修改背景色所以添加了background字段,但是又不起作用!!!1.找到values文件夹下面的themes文件夹,打......
  • android的基本控件TextView
    作用TextView(文本框),用于显示文本的一个控件属性详解<TextView android:id="@+id/txtOne"android:layout_width="wrap_content"android:layout_heigh......
  • Android-实现增加数据进入数据库
    继续写简单增删改查,终于可以实现插入数据进入数据库了主要代码差不多了,现在主要是需要解决一个显示数据的问题,这个问题之后接着写。初步效果  然后我们用可视化应用......
  • Android中drawable和mipmap到底有什么区别
    欢迎通过我的个人博客来查看此文章老项目代码中发现有的图片放到了drawable中,有的图片放到了mipmap中,开发时秉承哪个目录下文件多放哪里的原则,偶尔有疑惑搜一搜文......
  • android stdio中marqueeRepeatLimit无法循环播放
    首先,这个控件需要得到焦点,因此来实现循环播放,因此我们要 等我们再次启动虚拟机时,我们能够发现再次点击文字,就能够使控件文字循环播放。 但是这样点击文字循环会让整......
  • Android studio控件Imageview
      一:<ImageViewandroid:src="@drawable/yourname"//资源来源android:scaleType="centerInside"//缩放方式android:layout_width="200dp"//宽android:......
  • Android Studio 设置视图的间距
    首先区分两个类型marginpaddingmargin是指当前视图与平级视图只见的关系距离有layout_marginlayout_marginLeftlayout_marginToplayout_marginRightlayout_marginB......