首页 > 编程知识 正文

androidstudio预览布局(android线性布局两种形式)

时间:2023-05-06 16:34:34 阅读:66747 作者:4921

Android基础入门教程——2.2.2 RelativeLayout (相对布局选项卡)空格分隔) Android基础入门教程

在本节前一节中,详细分析了LinearLayout,但LinearLayout也是我们

的比较多的布局,让我们更迷恋他的微信属性,是等比分开,贴合屏幕,还是

有相当大的帮助; 但是,在使用LinearLayout情况下,在接口复杂的情况下,还存在需要多层嵌套的问题

线性布局会降低ui渲染的效率(渲染速度)。 另外,在listview和GridView上的

item,效率会变低。 此外,太多的线性布局嵌套可能会占用更多的系统资源,从而导致堆栈溢出

但是,使用RelativeLayout的话,也许只需要一楼就能完成。 请参阅父容器或同级组件中的margin

padding很有用,因为它可以设置组件的显示位置! 当然,不是绝对的。 具体问题具体分析一下吧。

总结一下是: 尽量使用RelativeLayout + LinearLayout的weight属性搭配使用吧。

1 .核心属性图

2 .父容器的定位属性示意图

3 .根据兄弟组合定位恩,先说一下什么是兄弟组合吧。 兄弟组件是指位于同一层次的容器组件。 如图所示

图中的组件1、2是兄弟组件,但组件3和组件1或组件2不是兄弟组件,因此组件3不能通过

用组件1或2定位。 例如,layout_toleftof=“组件1”会导致错误。 记住!

有关此同级组件定位的最典型示例是梅花布局,下面的代码实现:

运行效果图:

实现代码:

relativelayoutxmlns 3360 Android=' http://schemas.Android.com/apk/RES/Android ' xmlns 3360 tools=' http://方案. Android ' xmlns 360 to ools=' 3358方案. Android relative layout1' Android 3360 layout -它在容器的中央--- imageview Android 3360 id=' @ id/img1' Android 3360 layout _ width=' 80dp ' Android 3360 layout _ heid img2' Android 3360 layout _ width=' 80dp ' Android 3360 layout _ height=img1' Android 3360 layout _ center vertical=' trurual -中间图像的右侧--- imageview Android 3360 id=' @ id/img3 ' Android 3360 layout _ width=' 80dp ' Android : layout _ height=' 80dp ' Android 3360 layout _ torightof=' @ id -。 img4' Android 3360 layout _ width=' 80dp ' Android 3360 layout _ height=img1' Android 3360 layout _ center horizontal=' TTT

t; <!-- 在中间图片的下面 --> <ImageView android:id="@+id/img5" android:layout_width="80dp" android:layout_height="80dp" android:layout_below="@id/img1" android:layout_centerHorizontal="true" android:src="@drawable/pic5"/> </RelativeLayout> 4.margin与padding的区别

初学者对于这两个属性可能会有一点混淆,这里区分下:
首先margin代表的是偏移,比如marginleft = “5dp”表示组件离容器左边缘偏移5dp;
而padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字
比如为TextView设置paddingleft = “5dp”,则是在组件里的元素的左边填充5dp的空间!
margin针对的是容器中的组件,而padding针对的是组件中的元素,要区分开来!
下面通过简单的代码演示两者的区别:

比较示例代码如下:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/btn1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button"/> <Button android:paddingLeft="100dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_toRightOf="@id/btn1"/> <Button android:id="@+id/btn2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_alignParentBottom="true"/> <Button android:layout_marginLeft="100dp" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="Button" android:layout_toRightOf="@id/btn2" android:layout_alignParentBottom="true"/> </RelativeLayout>

运行效果图比较:

5.很常用的一点:margin可以设置为负数

相信很多朋友都不知道一点吧,平时我们设置margin的时候都习惯了是正数的,
其实是可以用负数的,下面写个简单的程序演示下吧,模拟进入软件后,弹出广告
页面的,右上角的cancle按钮的margin则是使用负数的!

效果图如下:

贴出的广告Activity的布局代码吧,当然,如果你对这个有兴趣的话可以下下demo,
因为仅仅是实现效果,所以代码会有些粗糙!

<RelativeLayout 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="com.jay.example.relativelayoutdemo.MainActivity" android:background="#00CCCCFF"> <ImageView android:id="@+id/imgBack" android:layout_width="200dp" android:layout_height="200dp" android:layout_centerInParent="true" android:background="@drawable/myicon" /> <ImageView android:id="@+id/imgCancle" android:layout_width="28dp" android:layout_height="28dp" android:layout_alignRight="@id/imgBack" android:layout_alignTop="@id/imgBack" android:background="@drawable/cancel" android:layout_marginTop="-15dp" android:layout_marginRight="-10dp" /> </RelativeLayout> 本节小结:

关于RelativeLayout的详解就到这里,有什么纰漏,错误,好的建议,欢迎提出~
最后提供下上面的demo代码供大家下载:RelativeLayoutDemo

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。