LinearLayout初步了解
来源:互联网 发布:魔兽世界7.0 数据库 编辑:程序博客网 时间:2024/05/29 18:39
布局:
1、文件位置res/layout/***.xml
2、确定布局管理器类型
3、在布局管理器中添加需要的组件
4、修改activity里指向的布局文件
5、所有的资源文件都必须小写
LinearLayout:
特点:每一行或者每一列中,只能放一个组件,当组件一个一个的排列到窗体的边缘后,剩下的组件不会被显示。在垂直线性布局管理器中,每一行只能放一个组件,在水平线性布局管理器中,每一列只能放一个组件。
方式:XML配置或者JAVA代码 推荐XML配置
常用属性:
android:orientation="horizontal | vertical":vertical垂直的,horizontal水平的
android:layout_width="wrap_content | match_parent": 包裹内容 | 填充父类空间
android:layout_height="match-parent":同上(fill_parent不推荐使用)
android:id="@+id/名称":设置id这个属性,可以在java文件中取到布局
android:background:图片或者颜色
android:gravity:控制布局控件中的对齐方式。如果没有子控件的控件设置此属性,表示其内容的对齐方式,比如说TextView里面文字的对齐方式,若有子控件的控件设置此属性,表示其子控件的对其方式,gravity如果需要设置多个属性,中间用"|"进行组合。
android:layout_weight="1":通过设置控件的layout_weight属性以控制各个控件在布局中的相对大小,线性布局会根据该控件layout_weight值与其所处布局中的所有控件layout_weight值之和的比值为该控件分配占用的区域。如果layout_weight值为0,控件会按原大小显示,不会被拉伸,对于其他layout_weight属性值大于0的控件,系统将会减去属性值为0的元素所占位置的大小,然后剩余的按比例分配
android:visibility=""visible | invisible | gone":显示 | 不显示但会占位置 | 不宣示也不占位置
android:gravity与android:layout_gravity的区别:
android:gravity:是指本元素的子元素相对它的对齐方式
android:layout_gravity:是指本元素相对它的父元素的对齐方式。
android:layout_margin***:设置本控件距离父控件的个数
android:layout_padding***:设置子控件距离父控件的距离
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll_up"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#ff00ff"
android:gravity="top"
android:orientation="horizontal"
android:padding="4dp" >
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:layout_weight="1"
android:text="第一列" />
<Button
android:id="@+id/btn_close"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_weight="1"
android:gravity="bottom|center"
android:padding="5dp"
android:text="关闭" />
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="第二列" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
利用java代码实现相同效果,不推荐
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private LinearLayout ll_parent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得父控件
ll_parent = (LinearLayout) findViewById(R.id.ll_parent);
// 新建新的LinearLayout控件
LinearLayout ll_up = new LinearLayout(this);
// 新建LinearLayout属性对象
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT, 1);
// 添加属性
ll_up.setLayoutParams(params);
// 设置方向
ll_up.setOrientation(LinearLayout.HORIZONTAL);
// 添加图片资源
ll_up.setBackgroundResource(R.drawable.fuck);
// 添加按钮1
// 新建一个按钮
Button btn_01 = new Button(this);
// 设置按钮的字体
btn_01.setText("按钮1");
// 新建按钮的属性
LayoutParams params2 = new LayoutParams(150, LayoutParams.MATCH_PARENT);
// 把属性添加到按钮中
btn_01.setLayoutParams(params2);
// 把按钮添加到父控件中
ll_up.addView(btn_01);
// 添加按钮
Button btn_02 = new Button(this);
btn_02.setText("按钮2");
btn_02.setLayoutParams(params2);
ll_up.addView(btn_02);
// 添加按钮
Button btn_03 = new Button(this);
btn_03.setText("按钮3");
btn_03.setLayoutParams(params2);
ll_up.addView(btn_03);
// 父控件中添加这个控件
ll_parent.addView(ll_up);
LinearLayout ll_down = new LinearLayout(this);
ll_down.setLayoutParams(params);
ll_down.setBackgroundResource(R.drawable.lj0601bz11);
ll_down.setOrientation(LinearLayout.VERTICAL);
// 添加按钮
Button btn_04 = new Button(this);
btn_04.setText("按钮4");
// 这次用的是父类型的类型
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 1);
btn_04.setLayoutParams(params4);
ll_down.addView(btn_04);
// 添加按钮
Button btn_05 = new Button(this);
btn_05.setText("按钮5");
btn_05.setLayoutParams(params4);
ll_down.addView(btn_05);
// 添加按钮
Button btn_06 = new Button(this);
btn_06.setText("按钮6");
btn_06.setLayoutParams(params4);
ll_down.addView(btn_06);
ll_parent.addView(ll_down);
}
}
0 0
- LinearLayout初步了解
- 深入了解LinearLayout
- SPRING 了解初步
- Jawe的初步了解
- Jawe的初步了解
- 初步了解osworkflow designer
- (二)初步了解
- 初步了解Oracle
- 初步了解CSS
- 初步了解CSS3
- Felix?OSGi? -初步了解
- 环境变量初步了解
- 初步了解接口测试
- skyeye初步了解
- Linux管道初步了解
- opencms初步了解
- 初步了解com
- Linux管道初步了解
- 清理心情,重新学习
- 数组的逆向输出
- VISTA或WIN7下使用立体声混音作为录音来源时,如何通过编程方式去除或加入麦克风的输入
- 黑马程序员——C语言学习笔记05 指针
- RelativeLayout初步了解
- LinearLayout初步了解
- Windows环境下的麦克风录音系统
- DrawerLayout初步了解
- VS中的解决方案设置--项目属性
- Java泛型
- hdoj 2042 不容易系列之二 C++
- js基本属性介绍
- Codeforces#304-D - Soldier and Number Game-求因子个数/数学
- Lambda表达式理解【匿名方法】