问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

android studio约束布局 如何制作阵列

发布网友 发布时间:2022-04-22 01:09

我来回答

2个回答

热心网友 时间:2023-07-19 02:36

ConstraintLayout(约束布局), 是2016年Google I/O最新推出的Android布局, 目前还在完善阶段. 从推出的力度而言, 应该会成为主流布局样式. 在最新版本的Android Studio中, ConstraintLayout已经成为默认布局.

概念

ConstraintLayout约束布局的含义: 根据布局中的其他元素或视图, 确定View在屏幕中的位置. 包含三个重要信息, 根据其他视图设置位置, 根据父容器设置位置, 根据基准线设置位置.

layout_constraint[本源]_[目标]="[目标ID]"

例如:

app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"

约束当前View的底部至目标View的底部, 目标View是constraintLayout. 表明, 把当前View放置到constraintLayout(父容器)的底部, 并且底部一致.

为了演示多个示例, 使用复用的Activity页面. 根据参数设置标题和布局Id.

public class LayoutDisplayActivity extends AppCompatActivity {    private static final String TAG = LayoutDisplayActivity.class.getSimpleName();    static final String EXTRA_LAYOUT_ID = TAG + ".layoutId"; // 布局ID     @Override protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setTitle(getIntent().getStringExtra(Intent.EXTRA_TITLE));        final int layoutId = getIntent().getIntExtra(EXTRA_LAYOUT_ID, 0);        setContentView(layoutId); // 设置页面布局, 复用布局    }}


主页面使用ListView展示多个项, 每项都是不同的布局. 点击项发送不同的Intent, 填充所要显示的页面.

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         ListView list = (ListView) findViewById(R.id.activity_main);        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,                android.R.layout.simple_list_item_1, LIST_ITEMS);        list.setAdapter(adapter);        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {                // 复用显示布局                Intent intent = new Intent(MainActivity.this, LayoutDisplayActivity.class);                intent.putExtra(Intent.EXTRA_TITLE, LIST_ITEMS[i]); // 标题                intent.putExtra(LayoutDisplayActivity.EXTRA_LAYOUT_ID, LAYOUT_IDS[i]); // 布局Id                startActivity(intent);            }        });    }}


基础

ConstraintLayout布局最基本的使用方式, 就是直接指定位置. 取消按钮的底部对齐constraintLayout(父容器)的底部, 左侧对齐父容器的左侧. 下一步按钮的底部对齐父容器的底部, 而左侧对齐取消按钮的右侧. 并且每个按钮边缘有Margin空隙.

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res/android"                                             xmlns:app="<a href="http://schemas.android.com/apk/res-auto" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res-auto"                                             android:id="@+id/constraintLayout"                                             android:layout_width="match_parent"                                             android:layout_height="match_parent">     <Button        android:id="@+id/cancel_button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="16dp"        android:layout_marginStart="16dp"        android:text="取消"        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"        app:layout_constraintStart_toStartOf="@id/constraintLayout"/>     <Button        android:id="@+id/next_button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="16dp"        android:layout_marginStart="16dp"        android:text="下一步"        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"        app:layout_constraintStart_toEndOf="@id/cancel_button"/> </android.support.constraint.ConstraintLayout>


ConstraintLayout的属性设置, 最重要的就是理解属性的表示含义.


Base

比例

ConstraintLayout布局除了对齐属性, 还有重要的比例属性. 中心(Center)按钮需要把全部边界与constraintLayout(父容器)边界对齐, 则为居中. 同时还可以设置水平与竖直的比例, 如0.25. Bias按钮设置水平与竖直的比例是0.25, 表示左侧与右侧比例是1:4, 上部与下部的比例是1:4.

constraintHorizontal_bias设置水平比例, constraintVertical_bias设置竖直比例.

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res/android"                                             xmlns:app="<a href="http://schemas.android.com/apk/res-auto" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res-auto"                                             xmlns:tools="<a href="http://schemas.android.com/tools" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/tools"                                             android:id="@+id/constraintLayout"                                             android:layout_width="match_parent"                                             android:layout_height="match_parent">     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Center"        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"        app:layout_constraintEnd_toEndOf="@id/constraintLayout"        app:layout_constraintStart_toStartOf="@id/constraintLayout"        app:layout_constraintTop_toTopOf="@+id/constraintLayout"/>     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Bias"        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"        app:layout_constraintEnd_toEndOf="@id/constraintLayout"        app:layout_constraintHorizontal_bias="0.25"        app:layout_constraintStart_toStartOf="@id/constraintLayout"        app:layout_constraintTop_toTopOf="@+id/constraintLayout"        app:layout_constraintVertical_bias="0.25"/> </android.support.constraint.ConstraintLayout>


tools:layout_editor_absoluteX属性对于视图起到辅助作用, 理解边界的真实距离, 点击可视化布局会自动生成.


Bias

引导线

ConstraintLayout布局除了与布局对齐以外, 还可以与引导线(Guideline)对齐. 设置竖直引导线(Guideline)距离左侧72dp. 按钮(Button)的左侧都与引导线对齐, 上下使用比例的方式排列, 一个0.25比例, 一个0.75比例.

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res/android"                                             xmlns:app="<a href="http://schemas.android.com/apk/res-auto" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res-auto"                                             xmlns:tools="<a href="http://schemas.android.com/tools" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/tools"                                             android:id="@+id/constraintLayout"                                             android:layout_width="match_parent"                                             android:layout_height="match_parent">     <android.support.constraint.Guideline        android:id="@+id/guideLine"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical"        app:layout_constraintGuide_begin="72dp"/>     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Guide Up"        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"        app:layout_constraintStart_toStartOf="@id/guideLine"        app:layout_constraintTop_toTopOf="@+id/constraintLayout"        app:layout_constraintVertical_bias="0.25"/>     <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Guide Down"        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"        app:layout_constraintStart_toStartOf="@id/guideLine"        app:layout_constraintTop_toTopOf="@+id/constraintLayout"        app:layout_constraintVertical_bias="0.75"/> </android.support.constraint.ConstraintLayout>


Guide Line

视图尺寸

ConstraintLayout布局中的控件也可以设置填充尺寸. 控件把宽度设置为0dp会自动根据位置进行填充. 如Large按钮, 左侧对齐与Small按钮的左侧, 右侧对齐与constraintLayout父控件的右侧, 宽度设置为0dp, 实际会填充全部位置.

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res/android"                                             xmlns:app="<a href="http://schemas.android.com/apk/res-auto" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res-auto"                                             android:id="@+id/constraintLayout"                                             android:layout_width="match_parent"                                             android:layout_height="match_parent">     <Button        android:id="@+id/small"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Small"        app:layout_constraintStart_toStartOf="@id/constraintLayout"        app:layout_constraintTop_toTopOf="@id/constraintLayout"/>     <Button        android:layout_width="0dp"        android:layout_height="wrap_content"        android:text="Large"        app:layout_constraintBottom_toBottomOf="@id/constraintLayout"        app:layout_constraintEnd_toEndOf="@id/constraintLayout"        app:layout_constraintStart_toEndOf="@id/small"        app:layout_constraintTop_toTopOf="@id/constraintLayout"/> </android.support.constraint.ConstraintLayout>


Measure

视图纵横比

ConstraintLayout布局还可以使用constraintDimensionRatio设置视图的纵横比, 则需要把宽(layout_width)或者高(layout_height)设置为0dp, 根据另一个属性和比例, 计算当前属性, 如两个图片控件的显示大小.

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res/android"                                             xmlns:app="<a href="http://schemas.android.com/apk/res-auto" "="" style="text-decoration: none; border-radius: 0px; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; overflow: visible; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto; box-sizing: content-box; font-size: 14px; min-height: inherit; color: rgb(92, 230, 56) !important; background: 0px 50%;">http://schemas.android.com/apk/res-auto"                                             android:id="@+id/constraintLayout"                                             android:layout_width="match_parent"                                             android:layout_height="match_parent">     <ImageView        android:layout_width="0dp"        android:layout_height="200dp"        android:background="@color/colorAccent"        android:src="@drawable/total_large"        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"        app:layout_constraintRight_toRightOf="@+id/constraintLayout"        app:layout_constraintTop_toTopOf="@+id/constraintLayout"        app:layout_constraintVertical_bias="0.0"/>     <ImageView        android:layout_width="0dp"        android:layout_height="200dp"        android:background="@color/colorAccent"        android:contentDescription="@null"        android:src="@drawable/total_large"        app:layout_constraintBottom_toBottomOf="@+id/constraintLayout"        app:layout_constraintDimensionRatio="4:3"        app:layout_constraintLeft_toLeftOf="@+id/constraintLayout"        app:layout_constraintRight_toRightOf="@+id/constraintLayout"/> </android.support.constraint.ConstraintLayout>

Ratio

ConstraintLayout约束布局的基本使用方式就是这些, 可以观察到ConstraintLayout布局兼顾LinearLayout与RelativeLayout的优点, 非常适合构建复杂布局, 会成为Android的主流布局方式.

热心网友 时间:2023-07-19 02:37

ConstraintLayout(约束布局), 是2016年Google I/O最新推出的Android布局, 目前还在完善阶段. 从推出的力度而言, 应该会成为主流布局样式. 在最新版本的Android Studio中, ConstraintLayout已经成为默认布局.
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
怎么养好四季海棠? 西安财神庙攻略,西安旅游攻略财神庙路线 漯河食品职业学院是几本 漯河食品职业学院是985还是211 做无痛人流的危害 地暖水压能保持多久 地暖水压维持时间有多长 海棠花开完花后该怎么处理 漯河食品职业学院为什么能升本 漯河食品职业学院质量检验系培养目标 漯河食品职业学院是谁建的 MAYA打开以后这样 显示不出模型 是什么情况 怎么解决 Android studio 开发app,如何抵抗动态调试,反调... android 中实现网页调用摄像头功能?怎么实现? android编程中如何实现新建activity 怎么烧红烧猪蹄 红烧猪蹄怎样做? outline+app是干什么的安全吗 红烧猪蹄怎样做好吃 红烧猪蹄怎么做味道好吃? 怎么做红烧猪肉 红烧猪肉如何做 重疾险保险 赔付比例 怎样做红烧猪蹄,红烧猪蹄做法 在保通买的重疾险险,有没有人知道理赔流程是怎么... 如何烧制红烧猪蹄 做红烧猪蹄怎么做 重疾险的赔付标准是怎么规定的 怎样做红烧猪肉 怎么做红烧猪肉? 买保险后,发生疾病怎么理赔 如何创建一个没有activity的应用 英语outline customization怎么翻译? maya2012中怎样把做出来的模型通过渲染做出二维边框线 一般common app上面的personal essay 写多少 soupui未显示outline选项卡 华为手机修一个屏幕要多少钱? 华为修屏幕要多少钱? ansysmechanical详细栏空白 H5+和mui开发的app,拍照完成后如何将拍到的照片在... 怎么删除 oracle 原有的outline 两篇生活日记,50字到一百字左右 2篇短短的日记.大概50字 日记50字十篇 三年级日记大全50字可以告诉我两篇吗? 日记50篇50字左右 日记两篇。100字或50字 写两篇50字左右的日记,标题《我的愿望》 写一篇景的日记50字 两篇小学生日记五十字以上一百字以下 清明节的两篇日记50字二年级的