android 线性布局LinearLayout实例代码

布局文件: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;
 }

}

最终效果:

时间: 2013-05-26

Android应用的LinearLayout中嵌套RelativeLayout的布局用法

想将Button和ListView分别放在屏幕的一左一右. 单纯使用android:gravity和android:layout_gravity不成功. 于是涉及到RelativeLayout. 关键为:android:layout_alignParentRight="true", android:layout_alignParentLeft="true": <?xml version="1.0" encoding="utf-8&

Android中LinearLayout布局的常用属性总结

基本属性要求 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> </LinearLayout> android:orientation 决定是水平排列或是垂直排列 vertical 垂直排列 horizontal 水平排列 垂直排列 Bu

Android App中使用LinearLayout进行居中布局的实例讲解

要想让您的控件水平居中或垂直居中其实很简单,只要在控件的上一级中设置[android:gravity="center"]属性即可 如: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center" android:backgro

android LinearLayout和RelativeLayout组合实现精确布局方法介绍

先明确几个概念的区别: padding margin都是边距的含义,关键问题得明白是什么相对什么的边距. padding是控件的内容相对控件的边缘的边距. margin是控件边缘相对父空间的边距.  android:gravity 属性是对该view 内容的限定.比如一个button 上面的text. 你可以设置该text 在view的靠左,靠右等位置.该属性就干了这个. android:layout_gravity是用来设置该view中的子view相对于父view的位置.比如一个button

Android使用LinearLayout设置边框

找到好多人的,都是文章随便copy,自己都不验证下,特别说LinearLayout中可以设置他的divider属性的,我在Android Studio中试了,根本显示不出来,这边是csdn上一个朋友回答的,我收藏了,放到这里,后面备用. 1.定一个underline的xml文件,把它放到drawable下 underline.xml <?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:a

android LinearLayout 布局实例代码

复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?>  <!--      <LinearLayout>         线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的   --> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"    

Android App中的多个LinearLayout嵌套布局实例解析

在做android  UI布局时,用了LinearLayout嵌套,发现效果并不如我预料一般 查了下资料,说是要设置layout_weight属性 资料说得不是很清楚,也没仔细看,就去弄,结果越弄越混乱. 于是静下心来,自己写xml测试,发现如下. 如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的, 换句话说最外层不能用layout_weight xml布局如下 <LinearLayout xmlns:android="http://schemas.

Android 布局控件之LinearLayout详细介绍

LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失.因此一个垂直列表的每一行只会有一个widget或者是container,而不管他们有多宽,而一个水平列表将会只有一个行高(高度为最高子控件的高度加上边框高度).LinearLayout保持其所包含的widget或者是container之间的间隔以及互相对齐(相对一个控件的右对齐.中间对齐或者左对齐). API说明

Android应用借助LinearLayout实现垂直水平居中布局

首先说的是LinearLayout布局下的居中一般是这样的: (注意:android:layout_width="fill_parent" android:layout_height="fill_parent" 属性中,若水平居中,至少在宽度上占全屏:若垂直居中,则在高度上占全屏) <LinearLayout android:layout_width="fill_parent" android:layout_height="fil

Android自定义控件LinearLayout实例讲解

很多时候Android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了.今天做了一个自定义控件的实例,来分享下. 首先定义一个layout实现按钮内部布局: <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=&

Android自定义控件样式实例详解

本文实例讲述了Android自定义控件样式的方法.分享给大家供大家参考,具体如下: Android控件样式自定义是用定义在drawable文件夹下的XML文件实现,在布局文件中通过设置控件的background属性达到效果. 一.控件常见状态:在XML文件中用到了selector节点,selector可以理解为状态切换器,不同的状态下切换不同的样式,各种状态用Item节点表示,以下为一些常见的状态(注意:statelist中第一个匹配当前状态的item会被使用.因此,如果第一个item没有任何状

Android VideoView类实例讲解

本节使用系统的示例类VideoView继续SurfaceView类相关内容的讲解,以让大家能更深入理解Android系统中图形绘制基础类的实现原理.也许你会发现无法改变VideoView类的控制方面,我们可以通过重构VideoView类来实现更加个性化的播放器.          下面是VideoView类的相关代码. Java 代码 public class VideoView extends SurfaceView implements MediaPlayerControl { privat

实例讲解Android自定义控件

小编在此之前给大家介绍过关于Android自定义控件的用法等,需要的可以参考下: Android开发之自定义控件用法详解 详解Android自定义控件属性 可以看到QQ上的ToolBar其实就是一个自定义的view,可以看到不同的界面就是简单地修改了文字而已,在第二张与第三张尤其的明显,我们就仿QQ的这个Toolbar设置一个自定义控件 在开始之前,首先了解一下官方是如何实现一个控件的,比如说一个Linearlayout 它不是有layout_width和layout_height这两个属性吗?

Android入门之LinearLayout、AbsoluteLayout的用法实例讲解

本文实例介绍了Android中LinearLayout.AbsoluteLayout的用法,希望能对于初学Android的朋友起到一点帮助作用.具体内容如下: Android 的UI 布局都以Layout 作为容器,并且在上面按照规定排列控件,这方面跟JAVA 的Swing 和LWUIT 很像.控件跟Layout 有很多属性是一样的,可以在Properties 里面修改,跟.NET/Delphi 等RAD 类似,其中最常用的属性有以下这些: id="@+id/edtInput",ID

实例讲解Android应用中自定义组合控件的方法

自定义view大概可以分成: 继承view 组合view 自己画的view(用paint和canvas) 自定义viewgroup(widget) 最近又跟同学聊起来了,于是就准备自己实现一个imagebutton来练练手.(最简单的了)以及后面一个把imageview与textview结合使用的案例. ImageButton 要实现ImageButton最容易想到的不就是把button和imageview合在一起么.嗯,人容易就想到这是一个组合自定义view.可是如何把一个image放到but

Android自定义控件eBook实现翻书效果实例详解

本文实例讲述了Android自定义控件eBook实现翻书效果的方法.分享给大家供大家参考,具体如下: 效果图: Book.java文件: package com.book; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; public class Book extend

Android Studio应用开发集成百度语音合成使用方法实例讲解

首先,语音合成是指将文本信息转换成声音.意思就是将文本转化为声音,让你的应用开口说话.国内在业内比较有名的第三方语音合成平台有百度语音和科大讯飞. 本文集成的是百度语音合成,其主要特点是: 完全永久免费 业界首创完全永久免费新形式,为开发者提供最流畅最自然的语音合成服务.完全免费,永久使用,彻底摆脱限制. 离线在线融合模式 SDK可以根据当前网络状况,自动判断使用本地引擎还是云端引擎进行语音合成,再也不用担心流量消耗! 多语言多音色可选 中文普通话.中英文混读.男声.女声任你选,更支持语速.音调

Android自定义控件下拉刷新实例代码

实现效果: 图片素材: --> 首先, 写先下拉刷新时的刷新布局 pull_to_refresh.xml: <resources> <string name="app_name">PullToRefreshTest</string> <string name="pull_to_refresh">下拉可以刷新</string> <string name="release_to_refre

Android Dialog对话框实例代码讲解

Dialog的基本方法 //创建Dialog AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //设置标题图标 builder.setIcon(R.drawable.ic_launcher); //设置标题 builder.setTitle("这是一个对话框"); //设置信息 builder.setMessage("是否要跳转?"); //确定按钮 setPosit