安卓如何实现线性布局

2025-11-03 15:11:15

1、打开我们的编辑软件eclipse软件,如图所示;

然后建立一个工程,至于怎么建工程,我相信小伙伴们应该知道怎么弄了;

安卓如何实现线性布局

2、找到文件目录,res-layout下的一个文件,然后选择,activity_main.xml。如图中所示,打开文档。

安卓如何实现线性布局

3、在整个界面中输入一下代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="fill_parent"  

    android:layout_height="fill_parent"

    android:orientation="horizontal">  

   

    <Button android:id="@+id/button1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="寻迹"

            android:layout_weight="1"

            />

    <Button android:id="@+id/button2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text=" 壁障"

            android:layout_weight="1" 

        />    

     <Button android:id="@+id/button3"

             android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:text=" 追踪"

             android:layout_weight="1" 

        />   

     <Button android:id="@+id/button4"

             android:layout_width="wrap_content"

             android:layout_height="wrap_content"

             android:text=" 学习"

             android:layout_weight="1" 

        />     

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world" 

        />

</LinearLayout>

接着保存;

安卓如何实现线性布局

4、点击如图所示的地方进行执行,我们的软件;

安卓如何实现线性布局

5、如图所示,我们的执行结果如下;

安卓如何实现线性布局

1、android:orientation          布局方向:"vertical"垂直线性布局,"horizontal"水平线性布局

android:id                   为控件指定相应的ID

android:text                 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符

android:grivity              指定控件的基本位置,比如说居中,居右等位置

android:textSize             指定控件当中字体的大小

android:background           指定该控件所使用的背景色,RGB命名法

android:width                指定控件的宽度

android:height               指定控件的高度

android:padding              指定控件的内边距,也就是说控件当中的内容

android:singleLine           如果设置为真的话,则将控件的内容在同一行当中进行显示   

android:layout_weight        默认值为0,layout_weight属性可以控制各个控件在布局中的相对大小,线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢