欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android 线性布局LinearLayout实例代码

程序员文章站 2023-12-04 21:10:22
布局文件:res/layout/activity_my.xml复制代码 代码如下:[html] 

布局文件:res/layout/activity_my.xml

复制代码 代码如下:

[html]  <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context=".myactivity" >

    <button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/wo"
        android:textcolorhint="@color/calamus" />

    <button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/shi" />

    <button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hao"
        android:textcolor="@color/calamus" />

    <button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/ren" />

</linearlayout>

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    tools:context=".myactivity" >

    <button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/wo"
        android:textcolorhint="@color/calamus" />

    <button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/shi" />

    <button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/hao"
        android:textcolor="@color/calamus" />

    <button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/ren" />

</linearlayout>


资源文件:res/values/strings.xml
复制代码 代码如下:

[html] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">linear</string>
    <string name="action_settings">settings</string>
    <string name="wo">我</string>
    <string name="shi">是</string>
    <string name="hao">好</string>
    <string name="ren">人</string>

</resources>

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">linear</string>
    <string name="action_settings">settings</string>
    <string name="wo">我</string>
    <string name="shi">是</string>
    <string name="hao">好</string>
    <string name="ren">人</string>

</resources>


资源文件:res/values/styles.xml
复制代码 代码如下:

[html] view plaincopyprint?<resources>

    <!--
        base application theme, dependent on api level. this theme is replaced
        by appbasetheme from res/values-vxx/styles.xml on newer devices.
    -->
    <style name="appbasetheme" parent="android:theme.light">
        <!--
            theme customizations available in newer api levels can go in
            res/values-vxx/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- application theme. -->
    <style name="apptheme" parent="appbasetheme">
        <!-- all customizations that are not specific to a particular api-level can go here. -->
    </style>
    <color name="calamus">#c77eb5</color>

</resources>

<resources>

    <!--
        base application theme, dependent on api level. this theme is replaced
        by appbasetheme from res/values-vxx/styles.xml on newer devices.
    -->
    <style name="appbasetheme" parent="android:theme.light">
        <!--
            theme customizations available in newer api levels can go in
            res/values-vxx/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- application theme. -->
    <style name="apptheme" parent="appbasetheme">
        <!-- all customizations that are not specific to a particular api-level can go here. -->
    </style>
    <color name="calamus">#c77eb5</color>

</resources>


资源索引文件r.java
复制代码 代码如下:

[java] view plaincopyprint?/* auto-generated file.  do not modify.
 *
 * this class was automatically generated by the
 * aapt tool from the resource data it found.  it
 * should not be modified by hand.
 */

package com.eirc.linear;

public final class r {
    public static final class attr {
    }
    public static final class color {
        public static final int calamus=0x7f070000;
    }
    public static final class dimen {
        /**  default screen margins, per the android design guidelines.

         customize dimensions originally defined in res/values/dimens.xml (such as
         screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.

         */
        public static final int activity_horizontal_margin=0x7f040000;
        public static final int activity_vertical_margin=0x7f040001;
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int linearlayout=0x7f090000;
        public static final int action_settings=0x7f090005;
        public static final int button1=0x7f090001;
        public static final int button2=0x7f090002;
        public static final int button3=0x7f090003;
        public static final int button4=0x7f090004;
    }
    public static final class layout {
        public static final int activity_my=0x7f030000;
    }
    public static final class menu {
        public static final int my=0x7f080000;
    }
    public static final class string {
        public static final int action_settings=0x7f050001;
        public static final int app_name=0x7f050000;
        public static final int hao=0x7f050004;
        public static final int ren=0x7f050005;
        public static final int shi=0x7f050003;
        public static final int wo=0x7f050002;
    }
    public static final class style {
        /**
        base application theme, dependent on api level. this theme is replaced
        by appbasetheme from res/values-vxx/styles.xml on newer devices.

 
            theme customizations available in newer api levels can go in
            res/values-vxx/styles.xml, while customizations related to
            backward-compatibility can go here.

 
        base application theme for api 11+. this theme completely replaces
        appbasetheme from res/values/styles.xml on api 11+ devices.

 api 11 theme customizations can go here.

        base application theme for api 14+. this theme completely replaces
        appbasetheme from both res/values/styles.xml and
        res/values-v11/styles.xml on api 14+ devices.

 api 14 theme customizations can go here.
         */
        public static final int appbasetheme=0x7f060000;
        /**  application theme.
 all customizations that are not specific to a particular api-level can go here.
         */
        public static final int apptheme=0x7f060001;
    }
}

/* auto-generated file.  do not modify.
 *
 * this class was automatically generated by the
 * aapt tool from the resource data it found.  it
 * should not be modified by hand.
 */


package com.eirc.linear;
复制代码 代码如下:

public final class r {
    public static final class attr {
    }
    public static final class color {
        public static final int calamus=0x7f070000;
    }
    public static final class dimen {
        /**  default screen margins, per the android design guidelines.

         customize dimensions originally defined in res/values/dimens.xml (such as
         screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.

         */
        public static final int activity_horizontal_margin=0x7f040000;
        public static final int activity_vertical_margin=0x7f040001;
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int linearlayout=0x7f090000;
        public static final int action_settings=0x7f090005;
        public static final int button1=0x7f090001;
        public static final int button2=0x7f090002;
        public static final int button3=0x7f090003;
        public static final int button4=0x7f090004;
    }
    public static final class layout {
        public static final int activity_my=0x7f030000;
    }
    public static final class menu {
        public static final int my=0x7f080000;
    }
    public static final class string {
        public static final int action_settings=0x7f050001;
        public static final int app_name=0x7f050000;
        public static final int hao=0x7f050004;
        public static final int ren=0x7f050005;
        public static final int shi=0x7f050003;
        public static final int wo=0x7f050002;
    }
    public static final class style {
        /**
        base application theme, dependent on api level. this theme is replaced
        by appbasetheme from res/values-vxx/styles.xml on newer devices.
  

            theme customizations available in newer api levels can go in
            res/values-vxx/styles.xml, while customizations related to
            backward-compatibility can go here.
      

        base application theme for api 11+. this theme completely replaces
        appbasetheme from res/values/styles.xml on api 11+ devices.

 api 11 theme customizations can go here.

        base application theme for api 14+. this theme completely replaces
        appbasetheme from both res/values/styles.xml and
        res/values-v11/styles.xml on api 14+ devices.

 api 14 theme customizations can go here.
         */
        public static final int appbasetheme=0x7f060000;
        /**  application theme.
 all customizations that are not specific to a particular api-level can go here.
         */
        public static final int apptheme=0x7f060001;
    }
}


activity:
复制代码 代码如下:

[java] view plaincopyprint?package com.eirc.linear;

import android.os.bundle;
import android.app.activity;
import android.view.menu;

public class myactivity extends activity {

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_my);
    }

    @override
    public boolean oncreateoptionsmenu(menu menu) {
        // inflate the menu; this adds items to the action bar if it is present. 
        getmenuinflater().inflate(r.menu.my, menu);
        return true;
    }

}

package com.eirc.linear;
复制代码 代码如下:

import android.os.bundle;
import android.app.activity;
import android.view.menu;

public class myactivity extends activity {

 @override
 protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_my);
 }

 @override
 public boolean oncreateoptionsmenu(menu menu) {
  // inflate the menu; this adds items to the action bar if it is present.
  getmenuinflater().inflate(r.menu.my, menu);
  return true;
 }

}


最终效果:

android 线性布局LinearLayout实例代码