LinearLayout初步了解

布局:
    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);
 
}
 
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值