转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39643669
LinearLayout是Android控件中的线性布局控件,它包含的子控件将以横向(HORIZONTAL)或竖向(VERTICAL)的方式排列,按照相对位置来排列所有的子控件及引用的布局容器。超过边界时,某些控件将缺失或消失。因此一个垂直列表的每一行只会有一个控件或者是引用的布局容器。
一、LinearLayout线性布局的相关属性说明:
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值之和的比值为该控件分配占用的区域。
二、LinearLayout项目演示3种实现方式示例
2.1 第一种实现方式:xml配置实现LinearLayout
<activity_main.xml>
<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"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello!"
android:textSize="20sp" />
</LinearLayout>
2.2 第二种实现方式:代码实现LinearLayout
<MainActivity.java>
</pre><pre name="code" class="java">/**
* @author liu
* @description 代码动态创建线性布局管理器
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LinearLayout layout = new LinearLayout(this);// 创建现行布局管理器
LinearLayout.LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);// 设置线性布局参数
layout.setOrientation(LinearLayout.VERTICAL);
TextView txt = new TextView(this);
LinearLayout.LayoutParams txtParams = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);// 设置组件参数
txt.setLayoutParams(txtParams);
txt.setText("Hello!");
txt.setTextSize(20);
layout.addView(txt, txtParams);
addContentView(layout, params);
}
}
2.3 第三种实现方式:自定义实现LinearLayout(继承LinearLayout)
2.3.1、实现效果图(图片旋转)
2.3.2、项目结构图
2.3.3、详细的编码实现
1)继承LinearLayout的子类文件MyLinearLayout.java:
public class MyLinearLayout extends LinearLayout {
/**
* 在xml布局文件中声名的view,创建时由系统自动调用。
*/
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
/**
* 初始化LinearLayout视图
*/
private void initView() {
// 设置LinearLayout的布局方向
setOrientation(LinearLayout.VERTICAL);
// 设置布局参数
LinearLayout.LayoutParams params = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(getContext());
tv.setText(R.string.hello_world);
// 在MyLinearLayout里面添加TextView
addView(tv, params);
for (int i = 0; i < 10; i++) {
ImageView iv = new ImageView(getContext());
iv.setImageResource(R.drawable.ic_launcher);
Animation animation1 = AnimationUtils.loadAnimation(getContext(),
R.anim.rotate);
iv.setAnimation(animation1);
// 在MyLinearLayout里面添加10个带动画的ImageView
addView(iv, params);
}
}
/**
* 对子view进行布局,确定子view的位置
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
}
/**
* 测量尺寸时的回调方法
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
2)主布局资源文件,activity_main.xml:
<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"
android:orientation="vertical" >
<com.liu.mylinearlayout01.MyLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3)动画文件rotate.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillBefore="false" >
<!--
旋转效果,pivotX,pivotY指旋转坐标;
fillAfter="true" fillBefore="false" 表示动画停止在最后的位置;
fromDegrees toDegrees从0°到350°
startOffset:延时1s执行
duration:动画执行时间3s
repeatCount: 重复次数3+1
-->
<rotate
android:duration="3000"
android:fromDegrees="0"
android:pivotX="50%p"
android:pivotY="20%p"
android:repeatCount="3"
android:startOffset="1000"
android:toDegrees="350" />
</set>
4)、主Activity程序入口类,MainActivity.java:无需修改(按Eclipse自动生成的代码即可)
以上就是笔者知道的LinearLayout的三种实现基本方式。
源码下载地址
分享到:
相关推荐
很多时候,线性布局需要嵌套其他布局,比如线性布局,相对布局等
Android线性布局实现聊天气泡Android线性布局实现聊天气泡Android线性布局实现聊天气泡Android线性布局实现聊天气泡Android线性布局实现聊天气泡Android线性布局实现聊天气泡
Android中常用的5大布局方式有以下几种:...线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平
Android开发学习23】界面布局之线性布局LinearLayout代码
该资源充分说明了Android中线性布局的使用方法。
帮助理解android的操作,线性布局是android最常见的布局,学会使用很有用。
• 为了让各种控件在不同的手机屏幕上都能运行良好,为了更好地管理Android应用的用户界面里的各种控件,Android提供了布局管理器。 • 使用布局管理器,可以根据运行平台来调整控件的大小,程序员要做的,就是选择...
用Android Studio 2.3.3做的线性布局例子,主要用到的标签有:<LinearLayout></LinearLayout>;主要用到的设置有:android:orientation="horizontal"、android:layout_weight="1"。
黑马66期android学习笔记19_线性布局的特性
线性布局(LinearLayout)是 Android 中最基本的布局之一,用于在界面上按照水平或垂直方向依次排列子视图。它是一种简单而灵活的布局方式,适用于许多应用场景,如列表项、工具栏、表单等等。 在 Android 开发中,...
Android用户界面设计:线性布局.pdf
Android 线性布局 实例,初学者非常有用。
使用表格布局与线性布局实现分类工具栏,android编程,代码很详尽
NULL 博文链接:https://sunzone.iteye.com/blog/1858186
android线性布局之计算器的界面实现.pdf
快速理解Android常用控件,线性布局设计及控件参数的设置,计算器,九宫格实现基础
android自动换行的线性布局.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
code 博文链接:https://limdengrock.iteye.com/blog/1018141
程序总体使用TabHost实现微信界面的四个模块,在每一个tab选项布局里使用自定义的ListView,添加ImageView和TextView控件,显示通讯录列表信息。自定义ListView的列表项布局文件,将每个ListView要显示的内容构成一个...
android线性布局之计算器的界面实现借鉴.pdf