Android三种基本布局方式


一个丰富的界面由很多个控件组成的,那么我们如何才能让各个控件有条不紊摆放在界面上,而不是乱糟糟的,这就需要借助布局来实现了
在这里插入图片描述

LinearLayout

LinearLayout又称作线性布局,是一种非常常用的布局

android:orientation 排列方式
在垂直方向上依次排列

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"/>
</LinearLayout>

在这里插入图片描述

水平上依次排列

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"/>
</LinearLayout>

在这里插入图片描述

android:layout_gravity
如果排列方式是vertical,只有在水平方向上的对齐方式才会生效
如果排列方式是horizontal,只有在垂直方向上的对齐方式才会生效

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="Button 1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="Button 2"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="Button 3"/>
</LinearLayout>

在这里插入图片描述

android:layout_weight
这个属性允许我们使用比例的方式来指定控件的大小

这里我们将EditText和Button的宽度都设置为了0dp,这是因为我们是用了android:layout_weight属性,这个时候控件的宽度就不应该由android:layout_width来决定了
android:layout_weight属性设定为1,表示EditText和Button将在水平方向平分宽度
在这里插入图片描述
为什么都指定为1就平分屏幕宽度呢?
系统会把LinearLayout下所有控件指定的layout_weight值相加,得到一个总值,然后每个控件所占大小的比例就是该控件的layout_weight值除以刚才算出的总值

仅指定EditText的android:layout_weight属性,并将Button的宽度改回了wrap_content,这种方式编写的界面,不仅可以适配各种屏幕,看起来也更加舒服

RelativeLayout

RelativeLayout又称作相对布局,它可以通过相对定位的方式让控件出现在布局的任何位置,因此RelativeLayout中的属性非常多

控件与父布局之间的定位

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button 1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Button 2"/>

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button 3"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:text="Button 4"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:text="Button 5"/>

</RelativeLayout>

在这里插入图片描述

控件与控件之间的定位

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Button 3"/>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/button3"
        android:layout_toLeftOf="@id/button3"
        android:text="Button 1"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/button3"
        android:layout_toRightOf="@id/button3"
        android:text="Button 2"/>

</RelativeLayout>

在这里插入图片描述

FrameLayout

又叫做帧布局,这种布局没有丰富的定位方式,所有的控件都会默认摆放在布局的左上角

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is TextView"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>
    
</FrameLayout>

在这里插入图片描述
按钮盖住了字体,可以通过android:layout_gravity定义位置

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:text="this is TextView"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="Button"/>

</FrameLayout>

在这里插入图片描述
由于定位方式的欠缺,FrameLayout的应用场景相对偏少一些~

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是 Android Studio 的布局基本代码和一个简单示例: XML布局代码: ```xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 一个 TextView --> <TextView android:id="@+id/textView" android:text="Hello World!" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!-- 一个 Button --> <Button android:id="@+id/button" android:text="Click Me!" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> ``` Java代码: ```java public class MainActivity extends AppCompatActivity { private TextView textView; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 TextView 和 Button 的实例 textView = (TextView) findViewById(R.id.textView); button = (Button) findViewById(R.id.button); // 设置 Button 的点击事件 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { textView.setText("Button Clicked!"); } }); } } ``` 这个示例中使用了 LinearLayout 布局,其中包含了一个 TextView 和一个 Button。在 Java 代码中获取了这两个控件的实例,并设置了 Button 的点击事件,当点击 Button 时会将 TextView 的文本设置为 "Button Clicked!"。 希望这个示例能够对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值