android 1B 学习

本文详细介绍了Android应用开发中常用的布局方式,包括线性布局LinearLayout、相对布局RelativeLayout及其使用方法。此外,还介绍了如何设置布局参数如wrap_content、match_parent以及自定义尺寸单位dp,并通过实例演示了如何利用layout_weight属性来分配子视图的空间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.ViewGroup
ViewGroup 可以包含多个view是放在其里面view的parent view
本身他也是一个view
//线性布局
Linear Layout 分为horizontal row
vertical row
xml语言的大致格式为

<LinearLayout
        android:orientation="vertical/horizontal"          //排列为竖直还是水平
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
  TextView<.
                   .
                   .
                                                            />

TextView<.
                  .
                  .
                                                            />

</LinearLayout>

2.wrap_content/match_parent/xxdp
wrap_content 这个参数是指把内容刚好包含所占的大小
match_parent这个参数是把子view的长或宽调成父窗口的大小
xxdp 是自定义窗口的大小

3.layout_weight
子窗口的权重,将height/width 设为0dp
权重越高分到的大小越大
//练习代码

<LinearLayout
     android:orientation="vertical"      
     android:layout_width="match_parent"
     android:layout_height="match_parent" >
   <ImageView
        android:src="@drawable/ocean"
        android:scaleType="centerCrop"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
    <TextView
    android:text="You're invited!"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="54sp"
    android:layout_weight="0"
    android:textColor="@android:color/black"
    android:background="#ccddff"
    />
     <TextView
    android:text="Bonfire at the bench"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0"
    android:textSize="34sp"
    android:fontFamily="sans-serif-light"
    android:textColor="@android:color/black"
    android:background="#ccddff"
    />
</LinearLayout>

4.RelativeLayout
android:layout_alignParentTop=”true”
android:layout_alignParentLeft=”true”
android:layout_alignParentRight=”true”
android:layout_alignParentBotton=”true”
默认左上角
锚定视图
android:layout_above=”@id/”
android:layout_toLeftOf=”@id/”
android:layout_toRightOf=”@id/”
android:layout_below=”@id/”
5.view id
android:”@+id/ydh_Text_View” (不得有任何空格)

6.padding and margin(默认为0)
android:padding=”8dp”
android:padding=”8dp”
android:padding=”8dp”
android:padding=”8dp”
android:marginTop=”8dp”
android:marginLeft=”8dp”
android:marginRight=”8dp”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值