前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >线性布局LinearLayout(二)

线性布局LinearLayout(二)

作者头像
李小白是一只喵
发布2020-04-24 08:46:35
4550
发布2020-04-24 08:46:35
举报
文章被收录于专栏:算法微时光

image.png

目录

LinearLayout

线性布局,顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的, 代码中加载主界面布局是从onCreate开始的:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

代码中的R.layout.activity_main就是添加的资源配置表.位置在:

image.png

现在的Android Studio支持拖拽添加控件,个人感觉不是很好使,尴尬..

image.png

点击LinearLayout添加一个horizontal类型的线性布局:

image.png

LinearLayout添加三个按钮:

image.png

切换到Design模式,看下效果:

image.png

修改后的xml文件如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <LinearLayout 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.example.activitylife.MainActivity"
        android:orientation="horizontal">


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

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

        <Button
            android:id="@+id/button7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>
为线性布局添加褐黑色北背景

在xml文档中添加代码:

image.png

代码:

代码语言:javascript
复制
android:background="#000000"

然后保存下,可以看到Android Studio中发生了变化:

image.png

编译后,运行看下效果:

image.png

基本和设计的一抹一样.O(∩_∩)O

参考

Android的学习第六章(布局一LinearLayout)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • LinearLayout
  • 参考
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档