• 一手遮天 Android


    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(布局类): RelativeLayout 相对布局

    示例如下:

    /view/layout/RelativeLayoutDemo1.java

    /**
     * RelativeLayout - 相对布局控件
     */
    
    package com.webabcd.androiddemo.view.layout;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.ViewGroup;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    public class RelativeLayoutDemo1 extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_layout_relativelayoutdemo1);
    
            // 演示如何在 java 中控制 RelativeLayout 布局,仅代码演示,没有对应的显示效果
            sample();
        }
    
        private void sample() {
            RelativeLayout relativeLayout = new RelativeLayout(this);
    
            TextView textView = new TextView(this);
            // 第 1 个参数对应 xml 中的 layout_width(像素值)
            // 第 2 个参数对应 xml 中的 layout_height(像素值)
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            // 对应 xml 中的 layout_...
            layoutParams.addRule(RelativeLayout.ABOVE, R.id.textView1); // layout_above
            layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, R.id.textView1); // layout_centerHorizontal
            textView.setLayoutParams(layoutParams);
    
            relativeLayout.addView(textView);
        }
    }
    
    

    /layout/activity_view_layout_relativelayoutdemo1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!--
        RelativeLayout - 相对布局控件
    -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:id="@+id/view1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@color/red" />
    
        <!--
            layout_above - 位于指定 id 控件的上方
            layout_below - 位于指定 id 控件的下方
        -->
        <TextView
            android:id="@+id/view2"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_below="@id/view1"
            android:background="@color/green" />
    
        <!--
            layout_alignStart - 与指定 id 控件的左边对齐(api level 17 或以上),相当于以前的 layout_alignLeft
            layout_alignEnd - 与指定 id 控件的右边对齐(api level 17 或以上),相当于以前的 layout_alignRight
            layout_alignTop - 与指定 id 控件的上边对齐
            layout_alignBottom - 与指定 id 控件的下边对齐
            layout_alignBaseline - 与指定 id 控件的基准线对齐
            layout_toStartOf - 位于指定 id 控件的左侧(api level 17 或以上),相当于以前的 layout_toLeftOf
            layout_toEndOf - 位于指定 id 控件的右侧(api level 17 或以上),相当于以前的 layout_toRightOf
        -->
        <TextView
            android:id="@+id/view3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignTop="@id/view2"
            android:layout_toEndOf="@+id/view2"
            android:background="@color/blue" />
    
        <!--
            layout_alignParentStart - 与父容器左侧对齐(api level 17 或以上),相当于以前的 layout_alignParentLeft
            layout_alignParentEnd - 与父容器右侧对齐(api level 17 或以上),相当于以前的 layout_alignParentRight
            layout_alignParentTop - 与父容器上侧对齐
            layout_alignParentBottom - 与父容器下侧对齐
            layout_centerInParent - 相对于父容器是否既水平居中又垂直居中
            layout_centerHorizontal - 相对于父容器是否水平居中
            layout_centerVertical - 相对于父容器是否垂直居中
        -->
        <TextView
            android:id="@+id/view4"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_alignParentEnd="true"
            android:layout_centerHorizontal="true"
            android:background="@color/orange" />
    
    
        <!--
            注:
            从上面的说明中可以看到在 api level 17 或以上新增了一些属性,就是把 Left 换成 Start,把 Right 换成 End,这是为了支持从右到左(RTL)的文字排列方式
            为了方便理解,我上面的说明都是按照文字从左到右排列的情况下解释的
        -->
    
    </RelativeLayout>
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    吃喝玩乐AI黑科技
    容器的介绍
    java线程池
    2020牛客暑期多校训练营(第三场)
    zabbix5.0快速搭建
    prometheus中的alertmanage配置文件详解
    gitlab的api操作
    vim编辑器可视模式
    sql语句执行顺序详解
    sql连表查询练习题
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_layout_RelativeLayoutDemo1.html
Copyright © 2020-2023  润新知